辅助线程如何与主UI线程通信? [英] How can worker threads communicate with main UI thread?

查看:159
本文介绍了辅助线程如何与主UI线程通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

工作线程与主UI线程进行通信的最佳方式是什么?

What is the best way for worker threads to communicate with the main UI thread?

摘要:我的C ++/MFC应用程序是基于对话框的.为了进行冗长的计算,主UI线程创建了多个工作线程.随着工作线程在计算中的进展,他们会将其进度报告给主UI线程,然后该主线程显示进度.

Summary: My C++/MFC application is dialog-based. To do lengthy computations, the main UI thread creates several worker threads. As the worker threads progress in the calculation, they report their progress to the main UI thread, which then displays progress.

这对于共享存储(由工作人员编写,由UI读取)中的数字进度值很好用,但是我在处理文本进度消息时遇到了麻烦.我尝试过的解决方案已经经过了多次迭代,但似乎都没有效果.

This works fine for numeric progress values, which are in shared memory (written by workers, read by UI), but I'm having trouble with text progress messages. My attempted solutions have been through several iterations, and none seems to work.

  1. 我让UI线程将指向控件的指针传递给工作人员,工作人员直接更新了UI.这不是很有效,而且似乎是错误的方法.

  1. I had the UI thread pass pointers to controls to the workers, and the workers updated the UI directly. This wasn't very effective, and seems like the wrong approach.

我让工作人员使用SendMessage将消息发送到UI线程的窗口.陷入僵局. (在处理完消息后,SendMessage才返回.)

I had the workers send messages, using SendMessage to the UI thread's window. This deadlocked. (SendMessage doesn't return until the message has been processed.)

与(2)相同,除了在UI线程的窗口中使用PostMessage.这工作了一段时间,然后消息丢失了. (PostMessage立即返回.)进一步的调查显示,消息队列的配额已超出,该配额默认为10,000.

Same as (2), except using PostMessage to the UI thread's window. This worked, for a while, then messages got lost. (PostMessage returns immediately.) Further investigation revealed the quota for message queues, which defaults to 10,000, was being exceeded.

我增加了消息队列的配额(注册表中的变量HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion \ Windows \ USERPostMessageLimit),但是丢失的消息数没有变化.

I increased the quota for message queues (variable HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows\USERPostMessageLimit in the registry), but the number of lost messages didn't change.

我在4 KB缓冲区中有每个工作线程缓冲区消息,在缓冲区满时有PostMessage消息.这失败了,因为UI线程从未收到任何消息.当我将缓冲区大小增加到64 KB时,情况也是如此.

I had each worker thread buffer messages in 4 KByte buffers, and PostMessage when a buffer filled. This failed because the UI thread never received any messages. Same was true when I increased the buffer size to 64 KBytes.

工作线程以最低"优先级运行,UI线程以正常"优先级运行.工作线程正在使用类似的代码发送消息

The worker threads are running at "lowest" priority, and the UI thread at "normal" priority. Worker threads are sending messages with code like

UIMessage *pUI=new UIMessage; // so it won't go out of scope (main dialog will delete it)
pUI->buffer=traceLineBuffer; pUI->nOutputs=traceN;
BOOL ok=::PostMessage(hWndMainDlg,TraceLineMsg,(WPARAM)pUI, NULL/*lParam*/);

UI正在使用类似的代码接收它们

and UI is receiving them with code like

BEGIN_MESSAGE_MAP(CMainDlg, CDialog)
...
ON_MESSAGE(TraceLineMsg,OnTraceLineMsg)
...
END_MESSAGE_MAP()

LRESULT CMainDlg::OnTraceLineMsg(WPARAM wParam, LPARAM lParam)
{
    UIMessage *pUI=(UIMessage *)wParam;
    char *p=pUI->buffer;
    // PROCESS BUFFER
    delete[] pUI->buffer;
    delete pUI;
    return 0;
}

问题:

  1. 在可能爆发成千上万个文本报告的情况下,工人发布进度报告的首选方式是什么?

  1. What is the preferred way for workers to issue progress reports, in a case where there may be bursts of several thousand text reports?

为什么不能增加队列中的帖子配额?

Why can I not increase the quota of post messages in a queue?

为什么主UI线程似乎永远不会在缓冲区中接收消息,即使传输消息的机制与发布单个报告的机制相同?

Why does the main UI thread seemingly never receive the messages in the buffers, even though the mechanism for transmitting them was identical to posting the individual reports?

64位Windows 7,Visual Studio 2010,本机C ++/MFC

64-bit Windows 7, Visual Studio 2010, native C++/MFC

推荐答案

使用WaitForMultipleObjects调用中的主线程将不会处理任何消息,也无法更新任何控件或其他窗口.解决方案是:不要这样做.

With the main thread in a WaitForMultipleObjects call no messages will be processed and no controls or other windows can be updated. The solution is: Don't do that.

这篇关于辅助线程如何与主UI线程通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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