VB.net 消息泵送至标签

发布于 2024-11-06 00:19:10 字数 842 浏览 0 评论 0原文

我基本上想实现一个标签并让它不断向用户显示信息。我希望能够执行以下操作:

someMethod():
    printMessage("Starting program")
    doWork() //possibly does some calls to printMessage()
    printMessage("Finished program")
end

printMessage(string message)
    mylabel.Text += message
end

并让 Windows 窗体上的标签不断显示该输出。也就是说,用户不必等到 someMethod() 完成并将所有信息突然转储到标签上,而是希望在信息出现时将其打印到标签上。

我尝试查看线程来解决这个问题,并且我使用类似的代码让它工作:(

someMethod():
    Dim t As New Thread(AddressOf printMessage)
    t.Start("Starting program")
    doWork()
    printMessage("Finished program")
end

并且有一个 printMessage 的委托,并且在内部我检查了 mylabel 的 InvokedRequired 属性)但是有了这个,我突然就不断地获取所有信息转储到标签上,并且订单不再保留。我可能会得到如下输出:

"some other data from doWork()"
"Finished program"
"Starting program"

那么我有什么想法可以实现这一点吗?

谢谢。

I basically want to implement a Label and have it constantly showing information to the user. I want to be able to do something like this:

someMethod():
    printMessage("Starting program")
    doWork() //possibly does some calls to printMessage()
    printMessage("Finished program")
end

printMessage(string message)
    mylabel.Text += message
end

And have the label on a Windows Form constantly be showing that output. That is, instead of the user having to wait until someMethod() is finished and having all the info suddenly dumped on to the label, I want it to be printed to the label as the information comes out.

I tried looking at threading to solve this problem, and I have it working using code something like:

someMethod():
    Dim t As New Thread(AddressOf printMessage)
    t.Start("Starting program")
    doWork()
    printMessage("Finished program")
end

(And there is a delegate for printMessage and inside I check the InvokedRequired property of mylabel) But with this, I keep getting all the information just suddenly dumped on to the label, and the order is no longer preserved. I may get output like:

"some other data from doWork()"
"Finished program"
"Starting program"

So any ideas how I can accomplish this?

Thanks.

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

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

发布评论

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

评论(2

拔了角的鹿 2024-11-13 00:19:10

您可能需要考虑使用 BackgroundWorker。它将使您可以轻松地在单独的线程上完成工作,并且仍然将进度报告回 UI 线程。

http://msdn.microsoft.com/en-us/library /system.componentmodel.backgroundworker.aspx

并使应用程序的其余部分保持响应。

BackGroundWorker 有一个 DoWork 事件,您可以在其中放置看起来(大致)如下所示的代码:

BGW.ReportProgress(0, "Starting Task1")
DoTask1()
BGW.ReportProgress(0, "Completed Task1")

BGW.ReportProgress(0, "Starting Task2")
DoTask2()
BGW.ReportProgress(0, "Completed Task2")

您还可以处理 ProgressChanged 事件,该事件将(在启动工作程序的线程上)触发以更新标签。

You might want to consider using a BackgroundWorker. It will make it easy for you to have the work done on a separate thread and still report progress back to the UI thread.

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

And have the rest of the app remain responsive.

The BackGroundWorker has a DoWork event where you could put code that looked (roughly) like this:

BGW.ReportProgress(0, "Starting Task1")
DoTask1()
BGW.ReportProgress(0, "Completed Task1")

BGW.ReportProgress(0, "Starting Task2")
DoTask2()
BGW.ReportProgress(0, "Completed Task2")

You'd also handle the ProgressChanged event which will fire (on the thread that started the worker) to update the label.

_失温 2024-11-13 00:19:10

调用 Application.DoEvents() 可能会有所帮助,因为它会强制窗口更新。

尝试将其放在每次 printMessage 调用之后。

A call to Application.DoEvents() may be beneficial, as it forces the window to update.

Try placing it right after each of your printMessage calls.

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