如何:实现支持基于事件的异步模式的组件

若要编写的类有一些可能会带来明显延迟的操作,请考虑按照基于事件的异步模式概述中的步骤操作,为它实现异步功能。

本演练展示了如何创建实现基于事件的异步模式的组件。 此组件是使用 System.ComponentModel 命名空间中的帮助程序类进行实现,这可确保它在任何应用模型(包括 ASP.NET、控制台应用和 Windows 窗体应用)下都能正常运行。 也可以使用 PropertyGrid 控件和自己的自定义设计器来设计此组件。

本演练使用异步计算质数的应用。 应用有主用户界面 (UI) 线程,以及用于每次质数计算的线程。 尽管测试大数字是否为质数需要花费很长时间,但主 UI 线程不会被此延迟中断,并且窗体在计算期间仍为响应式。 不仅可以同时运行计算(数量不限),还能选择性地取消挂起的计算。

本演练涉及以下任务:

  • 创建组件

  • 定义公共异步事件和委托

  • 定义专用委托

  • 实现公共事件

  • 实现完成方法

  • 实现工作方法

  • 实现启动和取消方法

若要将本主题中的代码复制为一个代码清单,请参阅如何:实现基于事件的异步模式的客户端

创建组件

第一步是,创建实现基于事件的异步模式的组件。

创建组件的具体步骤

  • 创建继承自 Component 的类 PrimeNumberCalculator

定义公共异步事件和委托

组件使用事件与客户端进行通信。 MethodNameCompleted 事件预警客户端注意异步任务完成,MethodNameProgressChanged 事件向客户端告知异步任务的进度。

若要定义组件客户端的异步事件,请执行以下步骤:

  1. 在文件顶部,导入 System.ThreadingSystem.Collections.Specialized 命名空间。

    using System;
    using System.Collections;
    using System.Collections.Specialized;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Globalization;
    using System.Threading;
    using System.Windows.Forms;
    
    Imports System.Collections
    Imports System.Collections.Specialized
    Imports System.ComponentModel
    Imports System.Drawing
    Imports System.Globalization
    Imports System.Threading
    Imports System.Windows.Forms
    
  2. PrimeNumberCalculator 类定义前面,声明进度和完成事件的委托。

    public delegate void ProgressChangedEventHandler(
        ProgressChangedEventArgs e);
    
    public delegate void CalculatePrimeCompletedEventHandler(
        object sender,
        CalculatePrimeCompletedEventArgs e);
    
    Public Delegate Sub ProgressChangedEventHandler( _
        ByVal e As ProgressChangedEventArgs)
    
    Public Delegate Sub CalculatePrimeCompletedEventHandler( _
        ByVal sender As Object, _
        ByVal e As CalculatePrimeCompletedEventArgs)
    
  3. PrimeNumberCalculator 类定义中,声明向客户端报告进度和完成事件的事件。

    public event ProgressChangedEventHandler ProgressChanged;
    public event CalculatePrimeCompletedEventHandler CalculatePrimeCompleted;
    
    Public Event ProgressChanged _
        As ProgressChangedEventHandler
    Public Event CalculatePrimeCompleted _
        As CalculatePrimeCompletedEventHandler
    
  4. PrimeNumberCalculator 类定义后面,派生 CalculatePrimeCompletedEventArgs 类,向 CalculatePrimeCompleted 事件的客户端事件处理程序报告每次计算的结果。 除了 AsyncCompletedEventArgs 属性外,客户端还可以使用此类确定测试的数字是什么、数字是否为质数,以及第一个除数是什么(如果不是质数的话)。

    public class CalculatePrimeCompletedEventArgs :
        AsyncCompletedEventArgs
    {
        private int numberToTestValue = 0;
        private int firstDivisorValue = 1;
        private bool isPrimeValue;
    
        public CalculatePrimeCompletedEventArgs(
            int numberToTest,
            int firstDivisor,
            bool isPrime,
            Exception e,
            bool canceled,
            object state) : base(e, canceled, state)
        {
            this.numberToTestValue = numberToTest;
            this.firstDivisorValue = firstDivisor;
            this.isPrimeValue = isPrime;
        }
    
        public int NumberToTest
        {
            get
            {
                // Raise an exception if the operation failed or
                // was canceled.
                RaiseExceptionIfNecessary();
    
                // If the operation was successful, return the
                // property value.
                return numberToTestValue;
            }
        }
    
        public int FirstDivisor
        {
            get
            {
                // Raise an exception if the operation failed or
                // was canceled.
                RaiseExceptionIfNecessary();
    
                // If the operation was successful, return the
                // property value.
                return firstDivisorValue;
            }
        }
    
        public bool IsPrime
        {
            get
            {
                // Raise an exception if the operation failed or
                // was canceled.
                RaiseExceptionIfNecessary();
    
                // If the operation was successful, return the
                // property value.
                return isPrimeValue;
            }
        }
    }
    
    
    Public Class CalculatePrimeCompletedEventArgs
        Inherits AsyncCompletedEventArgs
        Private numberToTestValue As Integer = 0
        Private firstDivisorValue As Integer = 1
        Private isPrimeValue As Boolean
    
    
        Public Sub New( _
        ByVal numberToTest As Integer, _
        ByVal firstDivisor As Integer, _
        ByVal isPrime As Boolean, _
        ByVal e As Exception, _
        ByVal canceled As Boolean, _
        ByVal state As Object)
    
            MyBase.New(e, canceled, state)
            Me.numberToTestValue = numberToTest
            Me.firstDivisorValue = firstDivisor
            Me.isPrimeValue = isPrime
    
        End Sub
    
    
        Public ReadOnly Property NumberToTest() As Integer
            Get
                ' Raise an exception if the operation failed 
                ' or was canceled.
                RaiseExceptionIfNecessary()
    
                ' If the operation was successful, return 
                ' the property value.
                Return numberToTestValue
            End Get
        End Property
    
    
        Public ReadOnly Property FirstDivisor() As Integer
            Get
                ' Raise an exception if the operation failed 
                ' or was canceled.
                RaiseExceptionIfNecessary()
    
                ' If the operation was successful, return 
                ' the property value.
                Return firstDivisorValue
            End Get
        End Property
    
    
        Public ReadOnly Property IsPrime() As Boolean
            Get
                ' Raise an exception if the operation failed 
                ' or was canceled.
                RaiseExceptionIfNecessary()
    
                ' If the operation was successful, return 
                ' the property value.
                Return isPrimeValue
            End Get
        End Property
    End Class
    

检查点 1

此时,可以生成组件。

测试组件的具体步骤

  • 编译组件。

    将看到下面两个编译器警告:

    warning CS0067: The event 'AsynchronousPatternExample.PrimeNumberCalculator.ProgressChanged' is never used  
    warning CS0067: The event 'AsynchronousPatternExample.PrimeNumberCalculator.CalculatePrimeCompleted' is never used  
    

    这些警告会在下一部分中得到清除。

定义专用委托

PrimeNumberCalculator 组件的异步特性是通过特殊的 SendOrPostCallback 委托在内部进行实现。 SendOrPostCallback 表示对 ThreadPool 线程执行的回调方法。 回调方法必须有需要使用单个 Object 类型参数的签名。也就是说,需要在包装类中的各委托之间传递状态。 有关详细信息,请参阅 SendOrPostCallback

若要实现组件的内部异步行为,请执行以下操作:

  1. PrimeNumberCalculator 类中声明并创建 SendOrPostCallback 委托。 在 InitializeDelegates 实用工具方法中创建 SendOrPostCallback 对象。

    需要使用两个委托:一个用于向客户端报告进度事件,另一个用于向客户端报告完成事件。

    private SendOrPostCallback onProgressReportDelegate;
    private SendOrPostCallback onCompletedDelegate;
    
    Private onProgressReportDelegate As SendOrPostCallback
    Private onCompletedDelegate As SendOrPostCallback
    
    protected virtual void InitializeDelegates()
    {
        onProgressReportDelegate =
            new SendOrPostCallback(ReportProgress);
        onCompletedDelegate =
            new SendOrPostCallback(CalculateCompleted);
    }
    
    Protected Overridable Sub InitializeDelegates()
        onProgressReportDelegate = _
            New SendOrPostCallback(AddressOf ReportProgress)
        onCompletedDelegate = _
            New SendOrPostCallback(AddressOf CalculateCompleted)
    End Sub
    
  2. 在组件的构造函数中调用 InitializeDelegates 方法。

    public PrimeNumberCalculator()
    {
        InitializeComponent();
    
        InitializeDelegates();
    }
    
    Public Sub New()
    
        InitializeComponent()
    
        InitializeDelegates()
    
    End Sub
    
  3. PrimeNumberCalculator 类中声明委托,以处理实际要异步完成的工作。 此委托包装用于测试数字是否为质数的工作方法。 此委托需要使用 AsyncOperation 参数,用于跟踪异步操作的生存期。

    private delegate void WorkerEventHandler(
        int numberToCheck,
        AsyncOperation asyncOp);
    
    Private Delegate Sub WorkerEventHandler( _
    ByVal numberToCheck As Integer, _
    ByVal asyncOp As AsyncOperation)
    
  4. 创建一个集合,用于管理挂起的异步操作的生存期。 客户端需要通过一种途径,跟踪已执行和完成的操作。若要执行这样的跟踪,客户端必须在调用异步方法时,传递唯一令牌或任务 ID。 PrimeNumberCalculator 组件必须跟踪所有调用,具体方法是将任务 ID 与其对应的调用相关联。 如果客户端传递的任务 ID 不唯一,PrimeNumberCalculator 组件必须抛出异常。

    PrimeNumberCalculator 组件使用特殊的集合类 HybridDictionary 跟踪任务 ID。 在类定义中,创建名为 userStateToLifetimeHybridDictionary

    private HybridDictionary userStateToLifetime =
        new HybridDictionary();
    
    Private userStateToLifetime As New HybridDictionary()
    

实现公共事件

实现基于事件的异步模式的组件使用事件与客户端进行通信。 这些事件在 AsyncOperation 类的相助下对适当的线程调用。

若要向组件的客户端抛出事件,请执行以下操作:

  1. 实现公共事件,以向客户端报告事件。 需要实现两个事件,一个用于报告进度事件,另一个用于报告完成事件。

    // 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);
    }
    
    // This method is invoked via the AsyncOperation object,
    // so it is guaranteed to be executed on the correct thread.
    private void ReportProgress(object state)
    {
        ProgressChangedEventArgs e =
            state as ProgressChangedEventArgs;
    
        OnProgressChanged(e);
    }
    
    protected void OnCalculatePrimeCompleted(
        CalculatePrimeCompletedEventArgs e)
    {
        if (CalculatePrimeCompleted != null)
        {
            CalculatePrimeCompleted(this, e);
        }
    }
    
    protected void OnProgressChanged(ProgressChangedEventArgs e)
    {
        if (ProgressChanged != null)
        {
            ProgressChanged(e);
        }
    }
    
    ' This method is invoked via the AsyncOperation object,
    ' so it is guaranteed to be executed on the correct thread.
    Private Sub CalculateCompleted(ByVal operationState As Object)
        Dim e As CalculatePrimeCompletedEventArgs = operationState
    
        OnCalculatePrimeCompleted(e)
    
    End Sub
    
    
    ' This method is invoked via the AsyncOperation object,
    ' so it is guaranteed to be executed on the correct thread.
    Private Sub ReportProgress(ByVal state As Object)
        Dim e As ProgressChangedEventArgs = state
    
        OnProgressChanged(e)
    
    End Sub
    
    Protected Sub OnCalculatePrimeCompleted( _
        ByVal e As CalculatePrimeCompletedEventArgs)
    
        RaiseEvent CalculatePrimeCompleted(Me, e)
    
    End Sub
    
    
    Protected Sub OnProgressChanged( _
        ByVal e As ProgressChangedEventArgs)
    
        RaiseEvent ProgressChanged(e)
    
    End Sub
    

实现完成方法

完成委托是在异步操作最终成功完成、出错或取消时,由基础的自由线程异步行为调用的方法。 此调用发生在任意线程上。

在此方法中,客户端任务 ID 从唯一客户端令牌的内部集合中删除。 另外,此方法还对相应的 AsyncOperation 调用 PostOperationCompleted方法,结束特定异步操作的生存期。 此调用对适用于应用模型的线程抛出完成事件。 调用 PostOperationCompleted 后,便无法再使用此 AsyncOperation 实例,随后只要尝试使用它,就会导致异常抛出。

CompletionMethod 签名必须保留描述异步操作结果所需的全部状态。 它保留以下状态:此异步操作测试的数字是什么、数字是否为质数,以及第一个除数的值是什么(如果不是质数的话)。 此外,它还保留描述所发生的任何异常的状态,以及与此任务对应的 AsyncOperation

若要完成异步操作,请执行以下操作:

  • 实现完成方法。 此方法需要使用六个参数,用于填充通过客户端的 CalculatePrimeCompletedEventHandler 返回到客户端的 CalculatePrimeCompletedEventArgs。 它还从内部集合中删除客户端的任务 ID 令牌,并通过调用 PostOperationCompleted 结束异步操作的生存期。 AsyncOperation 封送对适用于应用模型的线程或上下文执行的调用。

    // 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 is the method that the underlying, free-threaded 
    ' asynchronous behavior will invoke.  This will happen on
    '  an arbitrary thread.
    Private Sub CompletionMethod( _
        ByVal numberToTest As Integer, _
        ByVal firstDivisor As Integer, _
        ByVal prime As Boolean, _
        ByVal exc As Exception, _
        ByVal canceled As Boolean, _
        ByVal asyncOp As AsyncOperation)
    
        ' If the task was not previously canceled,
        ' remove the task from the lifetime collection.
        If Not canceled Then
            SyncLock userStateToLifetime.SyncRoot
                userStateToLifetime.Remove(asyncOp.UserSuppliedState)
            End SyncLock
        End If
    
        ' Package the results of the operation in a 
        ' CalculatePrimeCompletedEventArgs.
        Dim e As New CalculatePrimeCompletedEventArgs( _
            numberToTest, _
            firstDivisor, _
            prime, _
            exc, _
            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 PostOperationCompleted, asyncOp
        ' is no longer usable, and any attempt to use it will cause.
        ' an exception to be thrown.
    
    End Sub
    

检查点 2

此时,可以生成组件。

测试组件的具体步骤

  • 编译组件。

    将看到下面的一个编译器警告:

    warning CS0169: The private field 'AsynchronousPatternExample.PrimeNumberCalculator.workerDelegate' is never used  
    

    此警告会在下一部分中得到解析。

实现工作方法

至此,已实现 PrimeNumberCalculator 组件的支持异步代码。 现在,可以实现执行实际工作的代码。 将实现以下三个方法:CalculateWorkerBuildPrimeNumberListIsPrimeBuildPrimeNumberListIsPrime 共同构成了著名的埃拉托斯特尼筛法,通过在测试数字的平方根范围内查找所有质数,确定数字是否为质数。 如果使用这种方法没有找到任何除数,表明测试数字为质数。

如果此组件旨在最大限度地提高效率,便会记住对不同测试数字执行各种调用时发现的所有质数。 它还会检查是否有最简单的除数(如 2、3 和 5)。 虽然此示例旨在展示如何异步执行非常耗时的操作,但这些优化是留给大家练练手的。

CalculateWorker 方法包装在委托中,通过调用 BeginInvoke 进行异步调用。

注意

进度事件报告是在 BuildPrimeNumberList 方法中实现。 在快速运行的计算机上,ProgressChanged 事件可能会快速连续抛出。 对其抛出这些事件的客户端线程必须能够处理这种情况。 消息可能会像洪水般涌入用户界面代码,导致代码无法不断更新,继而导致无响应。 有关处理这种情况的示例用户界面,请参阅如何:实现基于事件的异步模式的客户端

若要异步执行质数计算,请执行以下操作:

  1. 实现 TaskCanceled 实用工具方法。 此方法检查任务生存期集合中是否有给定的任务 ID;如果找不到任务 ID,就会返回 true

    // Utility method for determining if a
    // task has been canceled.
    private bool TaskCanceled(object taskId)
    {
        return( userStateToLifetime[taskId] == null );
    }
    
    ' Utility method for determining if a 
    ' task has been canceled.
    Private Function TaskCanceled(ByVal taskId As Object) As Boolean
        Return (userStateToLifetime(taskId) Is Nothing)
    End Function
    
  2. 实现 CalculateWorker 方法。 它需要使用下面两个参数:要测试的数字和 AsyncOperation

    // This method performs the actual prime number computation.
    // It is executed on the worker thread.
    private void CalculateWorker(
        int numberToTest,
        AsyncOperation asyncOp)
    {
        bool isPrime = false;
        int firstDivisor = 1;
        Exception e = null;
    
        // Check that the task is still active.
        // The operation may have been canceled before
        // the thread was scheduled.
        if (!TaskCanceled(asyncOp.UserSuppliedState))
        {
            try
            {
                // Find all the prime numbers up to
                // the square root of numberToTest.
                ArrayList primes = BuildPrimeNumberList(
                    numberToTest,
                    asyncOp);
    
                // Now we have a list of primes less than
                // numberToTest.
                isPrime = IsPrime(
                    primes,
                    numberToTest,
                    out firstDivisor);
            }
            catch (Exception ex)
            {
                e = ex;
            }
        }
    
        //CalculatePrimeState calcState = new CalculatePrimeState(
        //        numberToTest,
        //        firstDivisor,
        //        isPrime,
        //        e,
        //        TaskCanceled(asyncOp.UserSuppliedState),
        //        asyncOp);
    
        //this.CompletionMethod(calcState);
    
        this.CompletionMethod(
            numberToTest,
            firstDivisor,
            isPrime,
            e,
            TaskCanceled(asyncOp.UserSuppliedState),
            asyncOp);
    
        //completionMethodDelegate(calcState);
    }
    
    ' This method performs the actual prime number computation.
    ' It is executed on the worker thread.
    Private Sub CalculateWorker( _
        ByVal numberToTest As Integer, _
        ByVal asyncOp As AsyncOperation)
    
        Dim prime As Boolean = False
        Dim firstDivisor As Integer = 1
        Dim exc As Exception = Nothing
    
        ' Check that the task is still active.
        ' The operation may have been canceled before
        ' the thread was scheduled.
        If Not Me.TaskCanceled(asyncOp.UserSuppliedState) Then
    
            Try
                ' Find all the prime numbers up to the
                ' square root of numberToTest.
                Dim primes As ArrayList = BuildPrimeNumberList( _
                    numberToTest, asyncOp)
    
                ' Now we have a list of primes less than 
                'numberToTest.
                prime = IsPrime( _
                    primes, _
                    numberToTest, _
                    firstDivisor)
    
            Catch ex As Exception
                exc = ex
            End Try
    
        End If
    
        Me.CompletionMethod( _
            numberToTest, _
            firstDivisor, _
            prime, _
            exc, _
            TaskCanceled(asyncOp.UserSuppliedState), _
            asyncOp)
    
    End Sub
    
  3. 实现 BuildPrimeNumberList。 它需要使用下面两个参数:要测试的数字和 AsyncOperation。 此方法使用 AsyncOperation 报告进度和增量结果。 这可确保对适用于应用模型的线程或上下文调用客户端的事件处理程序。 如果 BuildPrimeNumberList 找到了质数,它会将此作为增量结果,报告给 ProgressChanged 事件的客户端事件处理程序。 为此,必须使用派生自 ProgressChangedEventArgs 的类 CalculatePrimeProgressChangedEventArgs,其中新增有一个属性,即 LatestPrimeNumber

    BuildPrimeNumberList 方法还会定期调用 TaskCanceled 方法,并在此方法返回 true 时退出。

    // This method computes the list of prime numbers used by the
    // IsPrime method.
    private ArrayList BuildPrimeNumberList(
        int numberToTest,
        AsyncOperation asyncOp)
    {
        ProgressChangedEventArgs e = null;
        ArrayList primes = new ArrayList();
        int firstDivisor;
        int n = 5;
    
        // Add the first prime numbers.
        primes.Add(2);
        primes.Add(3);
    
        // Do the work.
        while (n < numberToTest &&
               !TaskCanceled( asyncOp.UserSuppliedState ) )
        {
            if (IsPrime(primes, n, out firstDivisor))
            {
                // Report to the client that a prime was found.
                e = new CalculatePrimeProgressChangedEventArgs(
                    n,
                    (int)((float)n / (float)numberToTest * 100),
                    asyncOp.UserSuppliedState);
    
                asyncOp.Post(this.onProgressReportDelegate, e);
    
                primes.Add(n);
    
                // Yield the rest of this time slice.
                Thread.Sleep(0);
            }
    
            // Skip even numbers.
            n += 2;
        }
    
        return primes;
    }
    
    ' This method computes the list of prime numbers used by the
    ' IsPrime method.
    Private Function BuildPrimeNumberList( _
        ByVal numberToTest As Integer, _
        ByVal asyncOp As AsyncOperation) As ArrayList
    
        Dim e As ProgressChangedEventArgs = Nothing
        Dim primes As New ArrayList
        Dim firstDivisor As Integer
        Dim n As Integer = 5
    
        ' Add the first prime numbers.
        primes.Add(2)
        primes.Add(3)
    
        ' Do the work.
        While n < numberToTest And _
            Not Me.TaskCanceled(asyncOp.UserSuppliedState)
    
            If IsPrime(primes, n, firstDivisor) Then
                ' Report to the client that you found a prime.
                e = New CalculatePrimeProgressChangedEventArgs( _
                    n, _
                    CSng(n) / CSng(numberToTest) * 100, _
                    asyncOp.UserSuppliedState)
    
                asyncOp.Post(Me.onProgressReportDelegate, e)
    
                primes.Add(n)
    
                ' Yield the rest of this time slice.
                Thread.Sleep(0)
            End If
    
            ' Skip even numbers.
            n += 2
    
        End While
    
        Return primes
    
    End Function
    
  4. 实现 IsPrime。 它需要使用下面三个参数:已知质数列表、要测试的数字,以及找到的第一个除数的输出参数。 它根据质数列表确定测试数字是否为质数。

    // This method tests n for primality against the list of
    // prime numbers contained in the primes parameter.
    private bool IsPrime(
        ArrayList primes,
        int n,
        out int firstDivisor)
    {
        bool foundDivisor = false;
        bool exceedsSquareRoot = false;
    
        int i = 0;
        int divisor = 0;
        firstDivisor = 1;
    
        // Stop the search if:
        // there are no more primes in the list,
        // there is a divisor of n in the list, or
        // there is a prime that is larger than
        // the square root of n.
        while (
            (i < primes.Count) &&
            !foundDivisor &&
            !exceedsSquareRoot)
        {
            // The divisor variable will be the smallest
            // prime number not yet tried.
            divisor = (int)primes[i++];
    
            // Determine whether the divisor is greater
            // than the square root of n.
            if (divisor * divisor > n)
            {
                exceedsSquareRoot = true;
            }
            // Determine whether the divisor is a factor of n.
            else if (n % divisor == 0)
            {
                firstDivisor = divisor;
                foundDivisor = true;
            }
        }
    
        return !foundDivisor;
    }
    
    ' This method tests n for primality against the list of 
    ' prime numbers contained in the primes parameter.
    Private Function IsPrime( _
        ByVal primes As ArrayList, _
        ByVal n As Integer, _
        ByRef firstDivisor As Integer) As Boolean
    
        Dim foundDivisor As Boolean = False
        Dim exceedsSquareRoot As Boolean = False
    
        Dim i As Integer = 0
        Dim divisor As Integer = 0
        firstDivisor = 1
    
        ' Stop the search if:
        ' there are no more primes in the list,
        ' there is a divisor of n in the list, or
        ' there is a prime that is larger than 
        ' the square root of n.
        While i < primes.Count AndAlso _
            Not foundDivisor AndAlso _
            Not exceedsSquareRoot
    
            ' The divisor variable will be the smallest prime number 
            ' not yet tried.
            divisor = primes(i)
            i = i + 1
    
            ' Determine whether the divisor is greater than the 
            ' square root of n.
            If divisor * divisor > n Then
                exceedsSquareRoot = True
                ' Determine whether the divisor is a factor of n.
            ElseIf n Mod divisor = 0 Then
                firstDivisor = divisor
                foundDivisor = True
            End If
        End While
    
        Return Not foundDivisor
    
    End Function
    
  5. ProgressChangedEventArgs 派生 CalculatePrimeProgressChangedEventArgs。 必须有此类,才能向 ProgressChanged 事件的客户端事件处理程序报告增量结果。 它新增有一个属性,即 LatestPrimeNumber

    public class CalculatePrimeProgressChangedEventArgs :
            ProgressChangedEventArgs
    {
        private int latestPrimeNumberValue = 1;
    
        public CalculatePrimeProgressChangedEventArgs(
            int latestPrime,
            int progressPercentage,
            object userToken) : base( progressPercentage, userToken )
        {
            this.latestPrimeNumberValue = latestPrime;
        }
    
        public int LatestPrimeNumber
        {
            get
            {
                return latestPrimeNumberValue;
            }
        }
    }
    
    Public Class CalculatePrimeProgressChangedEventArgs
        Inherits ProgressChangedEventArgs
        Private latestPrimeNumberValue As Integer = 1
    
    
        Public Sub New( _
            ByVal latestPrime As Integer, _
            ByVal progressPercentage As Integer, _
            ByVal UserState As Object)
    
            MyBase.New(progressPercentage, UserState)
            Me.latestPrimeNumberValue = latestPrime
    
        End Sub
    
        Public ReadOnly Property LatestPrimeNumber() As Integer
            Get
                Return latestPrimeNumberValue
            End Get
        End Property
    End Class
    

检查点 3

此时,可以生成组件。

测试组件的具体步骤

  • 编译组件。

    剩下要编写的就是,异步操作的启动和取消方法,即 CalculatePrimeAsyncCancelAsync

实现启动和取消方法

若要对它自己的线程启动工作方法,请对包装方法的委托调用 BeginInvoke。 若要管理特定异步操作的生存期,请对 AsyncOperationManager 帮助程序类调用 CreateOperation 方法。 这会返回 AsyncOperation,将对客户端事件处理程序的调用封送到适当的线程或上下文。

若要取消特定挂起操作,请对相应的 AsyncOperation 调用 PostOperationCompleted。 这样一来,可以结束操作,随后只要调用 AsyncOperation 都会导致异常抛出。

若要实现启动和取消功能,请执行以下操作:

  1. 实现 CalculatePrimeAsync 方法。 请确保相对于表示当前挂起任务的所有令牌,客户端提供的令牌(任务 ID)都是唯一的。 如果客户端传入的令牌不唯一,CalculatePrimeAsync 会抛出异常。 如果唯一,令牌会被添加到任务 ID 集合中。

    // This method starts an asynchronous calculation.
    // First, it checks the supplied task ID for uniqueness.
    // If taskId is unique, it creates a new WorkerEventHandler
    // and calls its BeginInvoke method to start the calculation.
    public virtual void CalculatePrimeAsync(
        int numberToTest,
        object taskId)
    {
        // Create an AsyncOperation for taskId.
        AsyncOperation asyncOp =
            AsyncOperationManager.CreateOperation(taskId);
    
        // Multiple threads will access the task dictionary,
        // so it must be locked to serialize access.
        lock (userStateToLifetime.SyncRoot)
        {
            if (userStateToLifetime.Contains(taskId))
            {
                throw new ArgumentException(
                    "Task ID parameter must be unique",
                    "taskId");
            }
    
            userStateToLifetime[taskId] = asyncOp;
        }
    
        // Start the asynchronous operation.
        WorkerEventHandler workerDelegate = new WorkerEventHandler(CalculateWorker);
        workerDelegate.BeginInvoke(
            numberToTest,
            asyncOp,
            null,
            null);
    }
    
    ' This method starts an asynchronous calculation. 
    ' First, it checks the supplied task ID for uniqueness.
    ' If taskId is unique, it creates a new WorkerEventHandler 
    ' and calls its BeginInvoke method to start the calculation.
    Public Overridable Sub CalculatePrimeAsync( _
        ByVal numberToTest As Integer, _
        ByVal taskId As Object)
    
        ' Create an AsyncOperation for taskId.
        Dim asyncOp As AsyncOperation = _
            AsyncOperationManager.CreateOperation(taskId)
    
        ' Multiple threads will access the task dictionary,
        ' so it must be locked to serialize access.
        SyncLock userStateToLifetime.SyncRoot
            If userStateToLifetime.Contains(taskId) Then
                Throw New ArgumentException( _
                    "Task ID parameter must be unique", _
                    "taskId")
            End If
    
            userStateToLifetime(taskId) = asyncOp
        End SyncLock
    
        ' Start the asynchronous operation.
        Dim workerDelegate As New WorkerEventHandler( _
            AddressOf CalculateWorker)
    
        workerDelegate.BeginInvoke( _
            numberToTest, _
            asyncOp, _
            Nothing, _
            Nothing)
    
    End Sub
    
  2. 实现 CancelAsync 方法。 如果令牌集合中有 taskId 参数,将会删除此参数。 这可以防止尚未启动的已取消任务运行。 如果任务正在运行,BuildPrimeNumberList 方法会在检测到任务 ID 已从生存期集合中删除时退出。

    // This method cancels a pending asynchronous operation.
    public void CancelAsync(object taskId)
    {
        AsyncOperation asyncOp = userStateToLifetime[taskId] as AsyncOperation;
        if (asyncOp != null)
        {
            lock (userStateToLifetime.SyncRoot)
            {
                userStateToLifetime.Remove(taskId);
            }
        }
    }
    
    ' This method cancels a pending asynchronous operation.
    Public Sub CancelAsync(ByVal taskId As Object)
    
        Dim obj As Object = userStateToLifetime(taskId)
        If (obj IsNot Nothing) Then
    
            SyncLock userStateToLifetime.SyncRoot
    
                userStateToLifetime.Remove(taskId)
    
            End SyncLock
    
        End If
    
    End Sub
    

检查点 4

此时,可以生成组件。

测试组件的具体步骤

  • 编译组件。

PrimeNumberCalculator 组件现已完成且可供使用。

有关使用 PrimeNumberCalculator 组件的示例客户端,请参阅PrimeNumberCalculator

后续步骤

可以编写与 CalculatePrimeAsync 方法相当的同步方法 CalculatePrime,扩充此示例。 这样一来,PrimeNumberCalculator 组件就完全符合基于事件的异步模式了。

若要改进此示例,可以保留对不同测试数字执行各种调用时发现的所有质数列表。 使用这种方法,所有任务都将受益于前面完成的任务。 请使用 lock 区域小心保护此列表,以序列化不同线程对列表的访问。

还可以测试是否有最简单的除数(如 2、3 和 5)来改进此示例。

另请参阅