进度“栏"在vb.net中使用线程 [英] Progress "bar" using threads in 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?
推荐答案
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.
这篇关于进度“栏"在vb.net中使用线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!