什么是C#相当于MsgWaitForMultipleObjects的? [英] What is the C# equivalent of MsgWaitForMultipleObjects?

查看:236
本文介绍了什么是C#相当于MsgWaitForMultipleObjects的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows窗体在报告模式一个ListView。对于视图中的每个项目,我需要执行长时间运行的操作,其结果是一个数字。

I have a Windows Form with a ListView in Report Mode. For each item in the view, I need to perform a long running operation, the result of which is a number.

我会为此在本地Win32的方法是创建的每一项工作线程(天真地;当然我不会创建线程无限多个)线程句柄数组,然后MsgWaitForMultipleObjects()。由于每次计算完成后,线程信号和主UI线程被唤醒和更新。与此同时,我们抽取消息所以UI线程仍然响应。

The way I would do this in native win32 is to create a worker thread for each item (naively; of course I won't create an unbounded number of threads) and then MsgWaitForMultipleObjects() on the array of thread handles. As each calculation finishes, the threads signal and the main UI thread wakes up and updates. In the mean time, we pump messages so the UI thread remains responsive.

任何人都可以提供如何,这可能在C#中工作的例子吗?我看了看监视器对象,而且它似乎并没有成为我想要什么,或者它抽取消息,同时阻止?

Can anyone provide an example of how this might work in C#? I've looked at the Monitor object, and it doesn't seem to be what I want—or does it pump messages while blocking?

感谢。

编辑:看来,WaitHandler.WaitAny()实际上可能抽取消息。请参见 cbrumme对消息中的CLR抽论文

It seems that WaitHandler.WaitAny() might actually pump messages. See cbrumme's treatise on message pumping in the CLR.

推荐答案

长期运行的活动对象,我认为是最好的选择你的情况。主线程调用(活动对象)的代理。代理转换调用方法的消息,此消息是要排队。代理返回给调用者将来对象(它是未来结果的引用)。调度出队的消息一个接一个,真的执行你的任务在其他线程(工作线程)。当工作线程完成一个任务,它更新了未来对象的结果,或者调用回调方法(例如,更新您的UI).Dispather可以有很多的工作线程在同一时间执行更多的任务。

The long-running active object, I think, is the best choose in your case. The main thread calls the proxy (of active object). Proxy converts the call method to the message and this message is going to a queue. Proxy returns to the caller the future object (it is a reference to the future result). The dispatcher dequeues messages one by one and realy executes your task in other thread (working thread). When working thread completes a task, it updates the result of the future object or calls callback method (for example, to update your UI).Dispather can have many working threads to execute more the one task at the same time.

您可以看到这个文章 (样本)有关长时间运行活动对象的模式。

You can see this article (with sample) about long-running active object pattern.

这篇关于什么是C#相当于MsgWaitForMultipleObjects的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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