扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:刘涛 来源:天极网 2007年11月9日
关键字:
Dim myThread As New Thread(AddressOf MyFunction) myThread.Start() |
图三、后台工作者组件使创建多线程应用程序更容易 |
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 |
’ 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 |
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 |
Private Sub backgroundWorker1_ProgressChanged( _ ByVal sender As Object, ByVal e As ProgressChangedEventArgs) _ Handles backgroundWorker1.ProgressChanged Me.progressBar1.Value = e.ProgressPercentage End Sub |
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 |
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 |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者