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

发布于 2024-11-27 07:26:30 字数 1589 浏览 2 评论 0原文

我是 VB.Net 新手,需要使用 While/End While 循环编写一个长时间运行的进程。

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

接下来,我需要更新 UI,但发现线程无法执行此操作。相反,线程必须调用 ReportProgress() 来触发 ProgressChanged() 事件。

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

这是代码:

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

I'm a VB.Net newbie, and need to write a long-running process with a While/End While loop.

To avoid freezing the UI, I added a BackgroundWorker object on the form.

Next, I needed to update the UI, but found that the thread cannot do this. Instead, the thread must call ReportProgress() to trigger the ProgressChanged() event.

However, I need to pass the text from the exception (ex.Message) to the event, but didn't find an example on how to do this. I need that text message to update the form's title bar.

Here's the code:

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

Thank you.


Edit: Here's how to get the pass the error message to the event, and change the form's title text:

        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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

双手揣兜 2024-12-04 07:26:30

Reportprogress 方法有一个重载,它需要 Int32 和< /em> 对象参数。您可以将消息或完整的异常传递给主线程。

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

The Reportprogress method has an overload that takes a Int32 and an Object parameter. You can pass the message or the complete Exception to the main Thread.

In the ProgressChanged event you can retrieve it from the UserState property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文