Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 如何将文本参数传递给ProgressChanged?_Vb.net_User Interface_Backgroundworker - Fatal编程技术网

Vb.net 如何将文本参数传递给ProgressChanged?

Vb.net 如何将文本参数传递给ProgressChanged?,vb.net,user-interface,backgroundworker,Vb.net,User Interface,Backgroundworker,我是一名VB.Net新手,需要编写一个带有While/End While循环的长时间运行的流程 为了避免冻结UI,我在表单上添加了一个BackgroundWorker对象 接下来,我需要更新UI,但发现线程无法做到这一点。相反,线程必须调用ReportProgress()来触发ProgressChanged()事件 但是,我需要将文本从异常(例如Message)传递到事件,但没有找到如何执行此操作的示例。我需要那条短信来更新表单的标题栏 代码如下: Private Sub BackgroundW

我是一名VB.Net新手,需要编写一个带有While/End While循环的长时间运行的流程

为了避免冻结UI,我在表单上添加了一个BackgroundWorker对象

接下来,我需要更新UI,但发现线程无法做到这一点。相反,线程必须调用ReportProgress()来触发ProgressChanged()事件

但是,我需要将文本从异常(例如Message)传递到事件,但没有找到如何执行此操作的示例。我需要那条短信来更新表单的标题栏

代码如下:

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    While (True)
        Try
            ...
        Catch ex As Exception
            'Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on.
            'Me.Text = ex.Message

            BackgroundWorker1.ReportProgress(100)
        End Try

        System.Threading.Thread.Sleep(2000)
    End While
End Sub

Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged

        'How to get ex.Message, and change the form's title bar accordingly?
        'Me.Text = ???
End Sub
多谢各位


编辑:以下是如何获取表单并将错误消息传递给事件,以及如何更改表单的标题文本:

        Catch ex As Exception
            BackgroundWorker1.ReportProgress(100, ex.Message)
        End Try

        System.Threading.Thread.Sleep(2000)
    End While
End Sub

Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    Me.Text = e.UserState.ToString
End Sub

Reportprogress方法具有接受Int32和对象参数的。您可以将消息或完整异常传递给主线程

在ProgressChanged事件中,可以从UserState属性检索它