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

 

 

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *