进度“条”在 VB.NET 中使用线程

发布于 2024-08-19 11:40:30 字数 175 浏览 7 评论 0原文

我目前有一个程序可以运行几个“密集”查询。我添加了一个文本框,并在查询开始时显示状态更新、编辑以及剩余数量。这可以满足我的需要,但在所有查询完成之前,文本框实际上不会显示任何内容。然后它会立即显示所有更新。我假设更新另一个线程中的文本框可以解决这个问题,这就是我迷失的地方。如何使用从运行查询的主窗体接收消息的线程并将其显示在文本框中?

I currently have a program that runs several "intense" queries. I added a textbox and presented status updates when a query was starting, eding and how many were left. This would suite my need, but the textbox doesn't actually display anything until all the queries are finished. It then displays all the updates at once. I'm assuming updating the textbox in another thread would solve this issue, and that is where I am lost. How can I use a thread that receives a message from the main form running the query and have it display it in the textbox?

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

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

发布评论

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

评论(4

旧人哭 2024-08-26 11:40:30

BackgroundWorker 组件 适合您的需要(示例代码位于 MSDN 链接中)。您处理其 DoWork 事件并在其中执行实际查询。您可以通过调用其 ReportProgress 方法来报告进度。要显示报告的进度,您应该处理其 ProgressChanged 事件并更新 UI。您可以通过调用后台工作线程的 RunWorkerAsync 方法来启动作业。使用 BackgroundWorker 使您无需手动启动和停止线程以及与 UI 线程通信以更新进度栏。

The BackgroundWorker component suits your need (sample code is there in the MSDN link). You handle its DoWork event and perform the actual query in it. You report the progress by calling its ReportProgress method. To display the reported progress, you should handle its ProgressChanged event and update the UI. You start the job by calling the RunWorkerAsync method of the background worker. Using BackgroundWorker relieves you from manually starting and stopping threads and communicating with the UI thread to update the progress bar.

心欲静而疯不止 2024-08-26 11:40:30

BackgroundWorker 是一种很好的通用方法,用于在后台线程上执行大量工作。但是,由于您的问题听起来像是您正在执行数据库操作,因此使用 对 ADO.Net 中的异步操作的本机支持。您可以使用回调作为进度条。

BackgroundWorker is a good general-purpose method for doing intensive work on a background thread. But, since your question sounds like you are doing database operations, it might be easier to use the native support for asynchronous operations in ADO.Net. You could use callbacks for the progress bar.

稍尽春風 2024-08-26 11:40:30

最简单的方法是使用BackgroundWorker,处理它的DoWork 事件并使用ProgressChanged 事件向ProgressBar 报告进度。

开始:

worker.RunAsync()

报告进度:

worker.ReportProgress(10) 'for 10%

The easiest way to do this is making use of BackgroundWorker, handling it's DoWork event and reporting progress to the ProgressBar with ProgressChanged event.

to start:

worker.RunAsync()

report progress:

worker.ReportProgress(10) 'for 10%
苏辞 2024-08-26 11:40:30

添加到 Mehrdad 和 Alex 发布的内容中,以下是如何处理由 ReportProgess 方法 (ProgressChanged) 引发的事件来更新进度栏:

Private Sub backgroundWorker_ProgressChanged ByVal sender As Object, ByVal e As ProgressChangedEventArgs) Handles backgroundWorker.ProgressChanged

    Me.progressBar1.Value = e.ProgressPercentage

End Sub

Adding to what Mehrdad and Alex posted, here's how to handle the event raised by the ReportProgess method (ProgressChanged) to update the progress bar:

Private Sub backgroundWorker_ProgressChanged ByVal sender As Object, ByVal e As ProgressChangedEventArgs) Handles backgroundWorker.ProgressChanged

    Me.progressBar1.Value = e.ProgressPercentage

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