// if a is 0, Begin_AddAsync will throw an exception. // if b is 0, End_AddAsync will throw an exception. public static IAsyncResult Begin_AddAsync(AsyncCallback cb, int a, int b, float delaySeconds) { if (a == 0) { throw new Exception("a is 0"); } var ar = new TestAsyncResult(); var t = new Thread(() => { Thread.Sleep((int)(delaySeconds * 1000)); ar.IsCompleted = true; if (b == 0) { ar.e = new Exception("b is 0"); } else { ar.v = a + b; } cb(ar); }); t.Start(); return(ar); }
public object EndAsyncOperation(IAsyncResult result) { TestAsyncResult testResult = (TestAsyncResult)result; this.CheckEnd(++this.EndCalls); return(testResult.Value); }
public void FromAsync_SimpleAsyncResult() { var result = new TestAsyncResult(); var factory = new TaskFactory <int> (); var task = factory.FromAsync(result, l => 5); Assert.IsTrue(task.Wait(1000), "#1"); Assert.AreEqual(5, task.Result, "#2"); }
public IAsyncResult BeginAsyncOperation(object value, AsyncCallback c, object state) { this.CheckBegin(++this.BeginCalls); var result = new TestAsyncResult { AsyncState = state, Value = value }; ThreadPool.QueueUserWorkItem(r => c((IAsyncResult)r), result); return result; }
public static IAsyncResult Begin_TooFastCallback( AsyncCallback cb, int arg ) { var ar = new TestAsyncResult(); ar.v = arg; ar.IsCompleted = true; // IMPORTANT: Coroutines see this flag to check whether a IAsyncResult has been completed already. cb( ar ); return ar; }
public static IAsyncResult Begin_TooFastCallback(AsyncCallback cb, int arg) { var ar = new TestAsyncResult(); ar.v = arg; ar.IsCompleted = true; // IMPORTANT: Coroutines see this flag to check whether a IAsyncResult has been completed already. cb(ar); return(ar); }
public void FromAsync_SimpleAsyncResult() { var result = new TestAsyncResult(); bool called = false; var task = factory.FromAsync(result, l => { called = true; }); Assert.IsTrue(task.Wait(1000), "#1"); Assert.IsTrue(called, "#2"); }
public IAsyncResult BeginAsyncOperation(object value, AsyncCallback c, object state) { this.CheckBegin(++this.BeginCalls); var result = new TestAsyncResult { AsyncState = state, Value = value }; ThreadPool.QueueUserWorkItem(r => c((IAsyncResult)r), result); return(result); }
public void FromAsync_ReturnInt() { var result = new TestAsyncResult(); bool called = false; var task = factory.FromAsync <int> (result, l => { called = true; return(4); }); Assert.IsTrue(task.Wait(1000), "#1"); Assert.IsTrue(called, "#2"); Assert.AreEqual(4, task.Result, "#3"); }
public void FromAsync_Scheduler_Explicit() { var result = new TestAsyncResult(); bool called = false; var scheduler = new TestScheduler(); var task = factory.FromAsync(result, l => { called = true; }, TaskCreationOptions.None, scheduler); Assert.IsTrue(task.Wait(5000), "#1"); Assert.IsTrue(called, "#2"); Assert.IsTrue(scheduler.ExecutedInline, "#3"); }
public void FromAsync_ResultException() { var result = new TestAsyncResult(); var task = factory.FromAsync(result, l => { throw new ApplicationException(); }); try { Assert.IsFalse(task.Wait(1000), "#1"); } catch (AggregateException) { } Assert.AreEqual(TaskStatus.Faulted, task.Status, "#2"); }
public void FromAsync_Scheduler_Implicit() { var result = new TestAsyncResult(); bool called = false; var scheduler = new TestScheduler(); factory = new TaskFactory(scheduler); Task task = factory.FromAsync(result, l => { called = true; }, TaskCreationOptions.AttachedToParent); Assert.AreEqual(TaskCreationOptions.AttachedToParent, task.CreationOptions, "#1"); Assert.IsNull(task.AsyncState, "#2"); Assert.IsTrue(task.Wait(5000), "#3"); Assert.IsTrue(called, "#4"); Assert.IsTrue(scheduler.ExecutedInline, "#5"); }
public void SyncedAsyncCallback_Invoked_ReceivesParameter() { TestAsyncResult parameter1 = new TestAsyncResult(); IAsyncResult arg1 = null; using (ActionThread thread = new ActionThread()) { thread.Start(); // Have the ActionThread set "action" AsyncCallback action = thread.DoGet(() => { return(Sync.SynchronizeAsyncCallback((IAsyncResult a1) => { arg1 = a1; })); }); action(parameter1); thread.Join(); Assert.AreSame(parameter1, arg1, "Action did not receive parameter"); } }
// if a is 0, Begin_AddAsync will throw an exception. // if b is 0, End_AddAsync will throw an exception. public static IAsyncResult Begin_AddAsync( AsyncCallback cb, int a, int b, float delaySeconds ) { if ( a == 0 ) throw new Exception( "a is 0" ); var ar = new TestAsyncResult(); var t = new Thread( () => { Thread.Sleep( (int)(delaySeconds * 1000) ); ar.IsCompleted = true; if ( b == 0 ) ar.e = new Exception( "b is 0" ); else ar.v = a + b; cb( ar ); }); t.Start(); return ar; }
public void FromAsync_SimpleAsyncResult () { var result = new TestAsyncResult (); var factory = new TaskFactory<int> (); var task = factory.FromAsync (result, l => 5); Assert.IsTrue (task.Wait (1000), "#1"); Assert.AreEqual (5, task.Result, "#2"); }
public void FromAsync_Scheduler_Implicit () { var result = new TestAsyncResult (); bool called = false; var scheduler = new TestScheduler (); factory = new TaskFactory (scheduler); Task task = factory.FromAsync (result, l => { Assert.IsTrue (Thread.CurrentThread.IsThreadPoolThread, "#6"); called = true; }, TaskCreationOptions.AttachedToParent); Assert.AreEqual (TaskCreationOptions.AttachedToParent, task.CreationOptions, "#1"); Assert.IsNull (task.AsyncState, "#2"); Assert.IsTrue (task.Wait (5000), "#3"); Assert.IsTrue (called, "#4"); Assert.IsTrue (scheduler.ExecutedInline, "#5"); }
public void FromAsync_Scheduler_Explicit () { var result = new TestAsyncResult (); bool called = false; var scheduler = new TestScheduler (); var task = factory.FromAsync (result, l => { called = true; }, TaskCreationOptions.None, scheduler); Assert.IsTrue (task.Wait (5000), "#1"); Assert.IsTrue (called, "#2"); Assert.IsTrue (scheduler.ExecutedInline, "#3"); }
public void FromAsync_ReturnInt () { var result = new TestAsyncResult (); bool called = false; var task = factory.FromAsync<int> (result, l => { called = true; return 4; }); Assert.IsTrue (task.Wait (1000), "#1"); Assert.IsTrue (called, "#2"); Assert.AreEqual (4, task.Result, "#3"); }
public void FromAsync_ResultException () { var result = new TestAsyncResult (); var task = factory.FromAsync (result, l => { throw new ApplicationException (); }); try { Assert.IsFalse (task.Wait (1000), "#1"); } catch (AggregateException) { } Assert.AreEqual (TaskStatus.Faulted, task.Status, "#2"); }
public void FromAsync_SimpleAsyncResult () { var result = new TestAsyncResult (); bool called = false; var task = factory.FromAsync (result, l => { called = true; }); Assert.IsTrue (task.Wait (1000), "#1"); Assert.IsTrue (called, "#2"); }
public void SyncedAsyncCallback_Invoked_ReceivesParameter() { TestAsyncResult parameter1 = new TestAsyncResult(); IAsyncResult arg1 = null; using (ActionThread thread = new ActionThread()) { thread.Start(); // Have the ActionThread set "action" AsyncCallback action = thread.DoGet(() => { return Sync.SynchronizeAsyncCallback((IAsyncResult a1) => { arg1 = a1; }); }); action(parameter1); thread.Join(); Assert.AreSame(parameter1, arg1, "Action did not receive parameter"); } }