Example #1
0
        // 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.
        }
Example #2
0
 protected void OnCalculatePrimeCompleted(CalculatePrimeCompletedEventArgs e)
 {
     if (CalculatePrimeCompleted != null)
     {
         CalculatePrimeCompleted(this, e);
     }
 }
Example #3
0
        // 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, CustomComponent.CalculatePrimeCompletedEventArgs e)
        {
            Guid taskId = (Guid)e.UserState;

            ListViewItem lvi = null;

            if (e.Cancelled)
            {
                string result = "Cancelled";

                lvi = UpdateListViewItem(taskId, result);

                if (lvi != null)
                {
                    lvi.BackColor = Color.Pink;
                    lvi.Tag       = null;
                }
            }
            else if (e.Error != null)
            {
                string result = "Error";
                lvi = UpdateListViewItem(taskId, result);

                if (lvi != null)
                {
                    lvi.BackColor = Color.Red;
                    lvi.ForeColor = Color.White;
                    lvi.Tag       = null;
                }
            }
            else
            {
                bool result = e.IsPrime;
                lvi = UpdateListViewItem(taskId, result, e.FirstDivisor);

                if (lvi != null)
                {
                    lvi.BackColor = Color.LightGray;
                    lvi.Tag       = null;
                }
            }
        }
Example #4
0
        // 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);
        }