// This method is invoked via the AsyncOperation object, // so it is guaranteed to be executed on the correct thread. private void CalculateCompleted(object operationState) { CalculatePrimeCompletedEventArgs e = operationState as CalculatePrimeCompletedEventArgs; OnCalculatePrimeCompleted(e); }
protected void OnCalculatePrimeCompleted( CalculatePrimeCompletedEventArgs e) { if (CalculatePrimeCompleted != null) { CalculatePrimeCompleted(this, e); } }
// This event handler updates the ListView control when the // PrimeNumberCalculator raises the CalculatePrimeCompleted // event. The ListView item is updated with the appropriate // outcome of the calculation: Canceled, Error, or result. private void primeNumberCalculator1_CalculatePrimeCompleted( object sender, CalculatePrimeCompletedEventArgs e) { Guid taskId = (Guid)e.UserState; if (e.Cancelled) { string result = "Canceled"; ListViewItem lvi = UpdateListViewItem(taskId, result); if (lvi != null) { lvi.BackColor = Color.Pink; lvi.Tag = null; } } else if (e.Error != null) { string result = "Error"; ListViewItem lvi = UpdateListViewItem(taskId, result); if (lvi != null) { lvi.BackColor = Color.Red; lvi.ForeColor = Color.White; lvi.Tag = null; } } else { bool result = e.IsPrime; ListViewItem lvi = UpdateListViewItem( taskId, result, e.FirstDivisor); if (lvi != null) { lvi.BackColor = Color.LightGray; lvi.Tag = null; } } }
// This is the method that the underlying, free-threaded // asynchronous behavior will invoke. This will happen on // an arbitrary thread. private void CompletionMethod( int numberToTest, int firstDivisor, bool isPrime, Exception exception, bool canceled, AsyncOperation asyncOp) { // If the task was not previously canceled, // remove the task from the lifetime collection. if (!canceled) { lock (userStateToLifetime.SyncRoot) { userStateToLifetime.Remove(asyncOp.UserSuppliedState); } } // Package the results of the operation in a // CalculatePrimeCompletedEventArgs. CalculatePrimeCompletedEventArgs e = new CalculatePrimeCompletedEventArgs( numberToTest, firstDivisor, isPrime, exception, canceled, asyncOp.UserSuppliedState); // End the task. The asyncOp object is responsible // for marshaling the call. asyncOp.PostOperationCompleted(onCompletedDelegate, e); // Note that after the call to OperationCompleted, // asyncOp is no longer usable, and any attempt to use it // will cause an exception to be thrown. }
// This event handler updates the ListView control when the // PrimeNumberCalculator raises the CalculatePrimeCompleted // event. The ListView item is updated with the appropriate // outcome of the calculation: Canceled, Error, or result. private void primeNumberCalculator1_CalculatePrimeCompleted( object sender, CalculatePrimeCompletedEventArgs e) { Guid taskId = (Guid)e.UserState; if (e.Cancelled) { string result = "Canceled"; ListViewItem lvi = UpdateListViewItem(taskId, result); if (lvi != null) { lvi.BackColor = Color.Pink; lvi.Tag = null; } } else if (e.Error != null) { string result = "Error"; ListViewItem lvi = UpdateListViewItem(taskId, result); if (lvi != null) { lvi.BackColor = Color.Red; lvi.ForeColor = Color.White; lvi.Tag = null; } } else { bool result = e.IsPrime; ListViewItem lvi = UpdateListViewItem( taskId, result, e.FirstDivisor); if (lvi != null) { lvi.BackColor = Color.LightGray; lvi.Tag = null; } } }
// This is the method that the underlying, free-threaded // asynchronous behavior will invoke. This will happen on // an arbitrary thread. private void CompletionMethod( int numberToTest, int firstDivisor, bool isPrime, Exception exception, bool canceled, AsyncOperation asyncOp) { // If the task was not previously canceled, // remove the task from the lifetime collection. if (!canceled) { lock (userStateToLifetime.SyncRoot) { userStateToLifetime.Remove(asyncOp.UserSuppliedState); } } // Package the results of the operation in a // CalculatePrimeCompletedEventArgs. CalculatePrimeCompletedEventArgs e = new CalculatePrimeCompletedEventArgs( numberToTest, firstDivisor, isPrime, exception, canceled, asyncOp.UserSuppliedState); // End the task. The asyncOp object is responsible // for marshaling the call. asyncOp.PostOperationCompleted(onCompletedDelegate, e); // Note that after the call to OperationCompleted, // asyncOp is no longer usable, and any attempt to use it // will cause an exception to be thrown. }
protected void OnCalculatePrimeCompleted( CalculatePrimeCompletedEventArgs e) { if (CalculatePrimeCompleted != null) { CalculatePrimeCompleted(this, e); } }