|
二、使用后台工作者的多线程例子
在Visual Basic 6.0中要解决多线程的问题如果不使用定时器的话几乎是不可能的事。在Visual Basic .NET中,就显得比较容易,只要创建一个thread对象,并给它传递一个你希望运行的方法,然后调用thread对象的开始方法就可以了。代码如下:
Dim myThread As New Thread(AddressOf MyFunction) myThread.Start() | 然而,强大的功能也意味着要承担巨大的责任。尽管Visual Basic .NET可以简单地创建并使用一个线程,但在开发程序时不得不小心谨慎,以免出现问题或BUG。
由于正确设计程序非常复杂,因此对于广大Visual Basic爱好者来说多线程并没有广泛地使用。然而,随着Visual Basic 2005的推出,由于使用了后台工作者组件,这个过程变的更容易而且更安全了。
为了说明创建多线程应用程序是多么容易,并且程序对用户的反应是多么灵敏,让我们创建一个名叫MultiThreaded的新窗体,窗体布局和代码同上,然而这次两个按钮分别命名为startAsyncButton、 cancelAsyncButton,因为这次我们将异步执行我们的代码,并且不阻塞主线程的运行。
对于我们的新窗体要做的第一件事是以设计模式打开它,并从工具箱的"组件"部分拖放一个后台工作者组件到窗口上。你还要在属性窗口中将该组件的WorkerReportProgress 、WorkerSupportsCancellation属性设置为"TRUE"。正如你将看到的,这些属性设置将允许我们更新进度条、终止进程。
 图三、后台工作者组件使创建多线程应用程序更容易 |
Private Sub startAsyncButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles startAsyncButton.Click ’ Reset the text in the result label. result.Text = [String].Empty ’ Disable the UpDown control until ’ the asynchronous operation is done. Me.numericUpDown1.Enabled = False ’ Disable the Start button until ’ the asynchronous operation is done. Me.startAsyncButton.Enabled = False ’ Enable the Cancel button while ’ the asynchronous operation runs. Me.cancelAsyncButton.Enabled = True ’ Get the value from the UpDown control. numberToCompute = CInt(numericUpDown1.Value) ’ Reset the variable for percentage tracking. highestPercentageReached = 0 ’ Start the asynchronous operation. backgroundWorker1.RunWorkerAsync(numberToCompute) End Sub | 你可能注意到我们将调用后台工作者组件的RunWorkerAsync方法,并在这之后省去了所有的代码。
当你想在一个独立的线程执行代码时,你可以调用RunWorkerAsync方法。这将在后台工作组件对象中产生DoWork事件。在这个事件中我们将计算斐波纳契数列值。
’ This event handler is where the actual work is done. Private Sub backgroundWorker1_DoWork( _ ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles backgroundWorker1.DoWork ’ Get the BackgroundWorker object that raised this event. Dim worker As System.ComponentModel.BackgroundWorker= CType(sender, System.ComponentModel.BackgroundWorker) ’ Assign the result of the computation ’ to the Result property of the DoWorkEventArgs ’ object. This is will be available to the ’ RunWorkerCompleted eventhandler. e.Result = ComputeFibonacci(e.Argument, worker, e) End Sub | 我们可以直接将代码放在这里来处理异步过程,但是在大多数情况下还是将其放在一个独立的程序中更好。在我们的例子中将使用现存的ComputeFibonacci,仅仅是作了一些小的改动而已。
Function ComputeFibonacci( ByVal n As Integer, _ ByVal worker As System.ComponentModel.BackgroundWorker, _ ByVal e As System.ComponentModel.DoWorkEventArgs) As Long ’ The parameter n must be >= 0 and <= 91. ’ Fib(n), with n > 91, overflows a long. If n < 0 OrElse n > 91 Then Throw New ArgumentException( "value must be >= 0 and <= 91", "n") End If Dim result As Long = 0 ’ Abort the operation if the user has canceled. ’ Note that a call to CancelAsync may have set ’ CancellationPending to true just after the ’ last invocation of this method exits, so this ’ code will not have the opportunity to set the ’ DoWorkEventArgs.Cancel flag to true. This means ’ that RunWorkerCompletedEventArgs.Cancelled will ’ not be set to true in your RunWorkerCompleted ’ event handler. This is a race condition. If worker.CancellationPending Then e.Cancel = True Else If n < 2 Then result = 1 Else result = ComputeFibonacci(n - 1, worker, e) + ComputeFibonacci(n - 2, worker, e) End If ’ Report progress as a percentage of the total task. Dim percentComplete As Integer = CSng(n) / CSng(numberToCompute) * 100 If percentComplete > highestPercentageReached Then highestPercentageReached = percentComplete worker.ReportProgress(percentComplete) End If End If Return result End Function | 开发人员在使用Visual Basic开发多线程程序时最常见的错误是试图操作另外线程创建的成员对象,例如用户界面线程。这将产生不可预期的、千奇百怪的错误,因为并不是每一个对象对于线程都是安全的。
第一件你将注意的事是我们将向后台工作者以及事件参数传递计算函数。这将允许我们在后台激活事件,主线程负责更新控件,如进程条等。
每次调用ComputeFibonacci时,我们将调用后台工作者组件的ReportProgress方法,每当我们这么作时组件的ProgressChanged事件将得到触发。这个事件将返回到主线程来更新进度条。为了避免线程交叉,我们没有在新的线程中更新进度条。
Private Sub backgroundWorker1_ProgressChanged( _ ByVal sender As Object, ByVal e As ProgressChangedEventArgs) _ Handles backgroundWorker1.ProgressChanged Me.progressBar1.Value = e.ProgressPercentage End Sub | 这时如果编译运行程序,你将发现即使你输入一个大的数字,也可以最大最小化应用程序,或是立即移动应用窗口。这是因为主线程用来响应用户操作而第二个线程用来进行计算。
因为主线程用来响应用户的操作,因此,我们可以在Cancel按钮后输入如下代码,来给用户提供退出应用程序的能力。
Private Sub cancelAsyncButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs)Handles cancelAsyncButton.Click ’ Cancel the asynchronous operation. Me.backgroundWorker1.CancelAsync() ’ Disable the Cancel button. cancelAsyncButton.Enabled = False End Sub | 注意到为了退出后台处理线程,我们只是简单地调用了后台工作者的CancelAsync方法,这个函数将组件的CancellationPending属性设置为TRUE,每次调用计算函数时都将对这个属性进行检查。
当计算函数完成了任务,后台工作者组件将抛出RunWorkerCompleted事件,在这个事件中我们的代码将显示计算结果、重置屏幕上的按钮,并为下次操作做准备。
Private Sub backgroundWorker1_RunWorkerCompleted( _ ByVal sender As Object, ByVal e As _ RunWorkerCompletedEventArgs) _ Handles backgroundWorker1.RunWorkerCompleted
’ First, handle the case where an exception was thrown. If Not (e.Error Is Nothing) Then MessageBox.Show(e.Error.Message) ElseIf e.Cancelled Then ’ Next, handle the case where the user canceled the ’ operation. result.Text = "Canceled" Else ’ Finally, handle the case where the operation succeeded. result.Text = e.Result.ToString() End If ’ Enable the UpDown control. Me.numericUpDown1.Enabled = True ’ Enable the Start button. startAsyncButton.Enabled = True ’ Disable the Cancel button. cancelAsyncButton.Enabled = False End Sub | 三、结论
我希望上述的例子代码已经向你显示了在Visual Basic 2005中使用后台工作组件的属性、方法和事件进行多线程编程是多么的容易。只要微小的一些修改,我们就可以创建一个交互式的应用程序,为了向用户提供最优秀并且高度互动的应用程序,你应该使用多线程,它将长时间运行的任务从用户界面线程中解脱出来。
[上一页] [1] [2]
|