public void WorkerThread_Blocking() { WorkerThread worker = new WorkerThread(); IAsyncResult ar1, ar2; DateTime start, finish1, finish2; try { // Invoke two operations, one that will wait 1 second // followed immediately by one that will wait another second. // The first operation should complete approximately one // second after starting and the second operation should // complete approximately two seconds after starting (because // it had to wait for the first operation to complete. start = SysTime.Now; ar1 = worker.BeginInvoke(new WorkerThreadMethod(Method4), 1050, null, null); ar2 = worker.BeginInvoke(new WorkerThreadMethod(Method4), 1050, null, null); finish1 = (DateTime)worker.EndInvoke(ar1); finish2 = (DateTime)worker.EndInvoke(ar2); Assert.IsTrue(finish1 - start >= TimeSpan.FromSeconds(1.0)); Assert.IsTrue(finish2 - start >= TimeSpan.FromSeconds(2.0)); } finally { worker.Close(); } }
public IAsyncResult BeginInvoke(AsyncCallback callback, Delegate method, object[] args, object asyncState) { if (fWorkerThread != null) { return(fWorkerThread.BeginInvoke(callback, method, args, asyncState)); } return(null); }
public void WorkerThread_Close() { WorkerThread worker = new WorkerThread(); IAsyncResult ar; try { ar = worker.BeginInvoke(new WorkerThreadMethod(Method3), null, null, null); Thread.Sleep(500); worker.Close(); worker.EndInvoke(ar); Assert.Fail("Expected a CancelException"); } catch (Exception e) { Assert.AreEqual(typeof(CancelException).Name, e.GetType().Name); } finally { worker.Close(); } }