为工作线程创建消息循环 [英] create message loop for worker thread

查看:82
本文介绍了为工作线程创建消息循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 我有一个要求,如下面的

  I have one requirement like the below

 我需要工作线程在状态栏上创建Icon,并且需要在频繁的时间间隔内更新图标(将显示剩余工作的百分比)。

  I need to have worker thread which creates Icon on the status bar and it needs to be update the icon (which will show percentage of work left ) in frequent time intervals.

 输入到更新图标将来自另一个线程(剩余多少工作)

 Inputs to update the Icon will come from another thread (how much work left)

 为了做到这一点,我可能需要创建一个带有消息循环的工作线程,第二个线程发布一条消息工作线程在收到消息时通知Icon的更新,工作线程更新图标,一旦看到"退出",它就退出。消息

 To do the above, I may need to create a worker thread with a message loop, second thread post a message to worker thread to notify the updates to Icon, worker thread updates icon when it receives a message, it quits once it sees "QUIT" message

我们将不胜感激任何帮助?

Any help would be appreciated?

谢谢,

保罗




  

  

  

  

推荐答案

必须在创建UI的线程上进行UI更新。控制 您无法使用工作线程来更新图像。 消息循环不能解决您的问题。

UI updates have to occur on the thread that created the UI control.  You cannot use a worker thread to update the images.  A message loop doesn't solve that problem for you.

您没有指定您正在使用的UI技术,因此我将介绍两种常用方法。

You didn't specify what UI technology you're using so I'll present the 2 common approaches.

1)
BackgroundWorker
- 此组件(BWC)(通常用于WinForms)旨在执行此类操作。 它有一个你将处理的DoWork活动。 此事件在工作线程上引发,因此您可以执行任何所需的工作,并且不会影响UI线程的
。 要更新UI,您将在BWC上使用ReportProgress方法。 这将处理从工作线程更新UI线程的详细信息。 您需要设置指示您将报告进度的属性,并且您需要UI
表格来监听该事件,否则它是自动的。 此外,如果您希望能够从UI停止工作,则可以配置BWC以支持取消。 最后,有一个完成事件,UI可以用来在工作完成时知道

1) BackgroundWorker - This component (BWC) (commonly used in WinForms) is designed to do this kind of thing.  It has a DoWork event that you will handle.  This event is raised on a worker thread so you can do any work you want and it won't impact the UI thread.  To update the UI you will use the ReportProgress method on the BWC.  This handles the details of updating the UI thread from the worker thread.  You need to set the property that indicates you'll report progress and you're UI form needs to listen for the event but otherwise it is automatic.  Additionally you can configure the BWC to support cancellation if you want to be able to stop the work from the UI.  Finally, there is a completion event that the UI can use to know when the work is complete.

2)从.NET v4.5开始,你可以使用
async
/等待完成类似的功能。 这虽然是TPL的反弹。 因此,您的工作不能保证在同一个线程上运行,但会在某个地方的后台线程上运行。 通过混合等待调用,您可以
等待后台工作,然后以相同的方法更新UI线程。 在后台,编译器将您的方法分解为可以在各种线程上调用的单独方法。 如果需要,您可以按
任意任务实施取消。 

2) Starting with .NET v4.5 you can use async/await to accomplish similar functionality.  This is bouncing off of the TPL though.  As such your work isn't guaranteed to run on the same thread but will be run on a background thread somewhere.  By mixing await with calls you can await the background work and then update the UI thread all in the same method.  Behind the scenes the compiler breaks up your method into separate methods that can be called on the various threads.  You can implement cancellation like you can for any Task if needed. 

我个人发现BWC是Winforms应用程序的更好方法,因为随着工作的进行,您只是随着进度更新UI。 异步更适合火灾和遗忘情况,或者当您需要将功能组合在一起时(在
背景上执行此操作,在UI上执行此操作,在背景上执行此操作等)。

Personally I find that BWC is the better approach for Winforms apps when you are simply updating the UI with progress as the work goes on.  Async works better for fire and forget situations or when you need to group functionality together (do this on background, do this on UI, do some more on background, etc).

Michael Taylor

http://blogs.msmvps.com/p3net

Michael Taylor
http://blogs.msmvps.com/p3net


这篇关于为工作线程创建消息循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆