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

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
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 itsDoWork
event and perform the actual query in it. You report the progress by calling itsReportProgress
method. To display the reported progress, you should handle itsProgressChanged
event and update the UI. You start the job by calling theRunWorkerAsync
method of the background worker. UsingBackgroundWorker
relieves you from manually starting and stopping threads and communicating with the UI thread to update the progress bar.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.
最简单的方法是使用BackgroundWorker,处理它的DoWork 事件并使用ProgressChanged 事件向ProgressBar 报告进度。
开始:
报告进度:
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:
report progress:
添加到 Mehrdad 和 Alex 发布的内容中,以下是如何处理由
ReportProgess
方法 (ProgressChanged
) 引发的事件来更新进度栏: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: