public void AsyncronousProgrammingModel_ExceptionInBeginInvoke() { //Arrange var workerWithException = new Helper01(PerformWorkHelper); workerWithException += PerformWorkHelper; var callbackOnEndOfWork = new AsyncCallback(EndCallback); //Act try { workerWithException.BeginInvoke(false, callbackOnEndOfWork, workerWithException); } catch (ArgumentException ex) { //Exceptions with BeginInvoke catched here //The asynchronously called delegates (MulticastDelegates) can only have one subscribed method Console.WriteLine(ex.Message); throw; } //Assert }
public void AsyncronousProgrammingModel_WaitOne() { //Arrange var worker = new Helper01(PerformWorkHelper); var callbackOnEndOfWork = new AsyncCallback(EndCallback); //Act //worker called with a bool param that makes it throw ex (catched in around EndInvoke) var result = worker.BeginInvoke(false, callbackOnEndOfWork, worker); //Do some other work in the main thread //Assert //Wait with the main thread until the worker finishes result.AsyncWaitHandle.WaitOne(); // If you release all references to the wait handle, system resources are freed when garbage collection reclaims the wait handle. // To free the system resources as soon as you are finished using the wait handle, // dispose of it by calling the WaitHandle.Close method. result.AsyncWaitHandle.Close(); Assert.IsTrue(result.IsCompleted); //We still need to wait for the callback //It can be done with the help of an AutoResetEvent instance _endCallbackStopWaitHandle.WaitOne(); }
public void AsyncronousProgrammingModel_ExceptionInWorker() { //Arrange var worker = new Helper01(PerformWorkHelper); var callbackOnEndOfWork = new AsyncCallback(EndCallback); //Act //worker called with a bool param that makes it throw ex (catched in around EndInvoke) worker.BeginInvoke(true, callbackOnEndOfWork, worker); //Assert Thread.Sleep(1000); }
public void AsyncronousProgrammingModel_EndInvokeCalledWrong() { //Arrange var worker = new Helper01(PerformWorkHelper); var callbackOnEndOfWork = new AsyncCallback(EndCallback); //Act var result3 = worker.BeginInvoke(false, callbackOnEndOfWork, worker); //The thread waits for result3 to execute here. ((Helper01)result3.AsyncState).EndInvoke(result3); //Assert //There is no need for wait or sleep because the EndInvoke blocks until the worker finishes //If you do not want to block the main thread than call it from the callback //However we need to wait till the callback is accessed //An exception resulted on the callback because EndInvoke can be called only once for each async worker Thread.Sleep(1000); }
public void AsyncronousProgrammingModel_HappyScenario() { //Arrange var worker = new Helper01(PerformWorkHelper); var callbackOnEndOfWork = new AsyncCallback(EndCallback); Console.WriteLine("Before BeginInvoke"); //Act //worker called with a bool param that makes it not to throw ex worker.BeginInvoke(false, callbackOnEndOfWork, worker); //Assert Console.WriteLine("After BeginInvoke"); // The callback is made on a ThreadPool thread. ThreadPool threads // are background threads, which do not keep the application running // if the main thread ends. Comment out the next line to demonstrate // this. Thread.Sleep(1000); }