线程中止后文本框未更新

时间:2019-03-05 00:21:31

标签: vb.net multithreading asynchronous

我有一个线程正在执行一个子程序,并且在一个子程序中,我正在“ Do while”循环过程中更新文本框。然后,我创建了一个取消按钮来中止线程。

单击按钮后,线程将中止,但文本框(Status_Txtbox.Text)不会更新为消息“解析已终止”。我尝试进行调试,我发现代码执行得很好,并且thread.isalive的条件是否满足,但不确定为什么文本框未随消息更新。

是否知道一旦线程中止如何更新文本框?

Dim thrd1 As Thread
Private Sub Parse_Btn_2G_Click(sender As Object, e As RoutedEventArgs) Handles Parse_Btn_2G.Click
    Parse_Btn_2G.IsEnabled = False
    Scan_Btn_2G.IsEnabled = False
    cancel_Btn_2G.IsEnabled = True
    Dim start As DateTime = DateTime.Now
    Dim elapsedtime As Double
    Dim action As Action
    thrd1 = New Thread(Sub()
                           ButtonClickWork.DoWork()
                           action = Sub()
                                        Status_Txtbox.Text = "Parsing Data, Please wait..."
                                    End Sub
                           Me.Dispatcher.Invoke(action)
                       End Sub)
    thrd1.Start()
    elapsedtime = (DateTime.Now.Subtract(start).TotalSeconds) / 60
    elapsedtime = Math.Round(elapsedtime, 2)
    Status_Txtbox.Text = " Managed Objects in XML, total time elapsed is" & elapsedtime
End Sub
  Private Sub Cancel_Btn_2G_Click(sender As Object, e As RoutedEventArgs) Handles cancel_Btn_2G.Click
    Try
        If cancel_Btn_2G.IsEnabled = True Then
            If MsgBox("Do you really want to exit Parsing?", vbYesNo) = MsgBoxResult.Yes Then
                Parse_Btn_2G.IsEnabled = True
                Scan_Btn_2G.IsEnabled = True
                cancel_Btn_2G.IsEnabled = False
                thrd1.Abort()
                thrd1.Join()
                If thrd1.IsAlive = False Then
                    Status_Txtbox.Text = "Parsing is terminated"
                End If
            End If
        End If
    Catch ex As ThreadAbortException
        Status_Txtbox.Text = "Parsing is terminated"
    End Try
End Sub

0 个答案:

没有答案