多线程编程:从线程包装器到 BackgroundWorker
在多线程编程中,我们常常需要处理一些耗时的任务,以避免阻塞主线程,影响用户体验。本文将介绍两种多线程编程的方法:使用线程包装器(Thread Wrapper)和 BackgroundWorker 组件。
线程包装器的使用
线程包装器是一种封装线程操作的方式,它可以帮助我们更方便地管理线程。下面是一个使用线程包装器进行质数搜索的示例。
1. 线程包装器类的设计
Protected Overrides Sub DoTask()
' Find the primes between fromNumber and toNumber,
' and return them as an array of integers.
' (See the code in the downloadable examples.)
End Sub
Public Event Completed As EventHandler(Of FindPrimesCompletedEventArgs)
Protected Overrides Sub OnCompleted()
' Signal that the operation is complete.
If CompletedEvent IsNot Nothing Then
RaiseEvent Completed(Me, _
New FindPrimesCompletedEventArgs(fromNumber, toNumber, primeList)
超级会员免费看
订阅专栏 解锁全文
1万+

被折叠的 条评论
为什么被折叠?



