Use ManualResetEvent to allow you to wait on your thread with an anync task completes.

Global object

	private ManualResetEvent OurTxIsDone;
Constructor

	OurTxIsDone = new ManualResetEvent(false);
Using It In A Method That Does Something Async Example

		//Inline event handler for the Completed event (We've implemented inline in order to make this method self-contained)
		MyOtherObjectThatsGoingToDoSomethingAsync.Completed += new EventHandler<SocketAsyncEventArgs>(delegate (object s, SocketAsyncEventArgs e)
		{
			//response = e.SocketError.ToString();	//If you wanted to get any error string
			OurTxIsDone.Set();					//Release the original thread wait state (below)
		});

		//Set the state of the event to nonsignaled, causing threads to block
		OurTxIsDone.Reset();

		//Start the asynchronous Send request
		MyOtherObjectThatsGoingToDoSomethingAsync.SendToAsync(1234);

		//WAIT on this thread for it to complete
		OurTxIsDone.WaitOne(5000);            //mS timeout

 

 

UK Electronic Designer
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.