将消息从不同的线程发布到主线程 [英] posting messages tomain thread from different thread

查看:68
本文介绍了将消息从不同的线程发布到主线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++/CLI类 ASController ,该类在主线程上运行,该主线程启动了后台线程.后台线程回调到C ++类 NotificationThunks :: ASNotification .我在回调中将ASController对象作为void * inUserData接收. 我想在回调中调用 ASController :: mainEvent()函数.但是,我不想直接使用对象实例进行调用,而是希望将消息发布到主窗口以触发ASController :: mainEvent,以便控制切换到主线程,并且后台线程将继续运行而无需等待.
我怎样才能做到这一点?如果我的两个类都在C ++/CLI中,那么会容易一些.但是我不知道该怎么做.

I have a C++/CLI class ASController that runs on the main thread, which starts a background thread. The background thread calls back to C++ class NotificationThunks::ASNotification. I recieve the ASController object in the callback as void* inUserData.
I want to call the ASController::mainEvent() function in the callback. But instead of calling directly using object instance, i want to post message to the main window to trigger the ASController::mainEvent, so that the control switches to main thread and background thread would continue without waiting.
How can I do this? If both of my classes are in C++/CLI it would be little easier. But I couldn''t figure out how to do this in my case.

class NotificationThunks
{
   public:
     NotificationThunks(){};
     static void ASNotification(CASMark *pMark, void *inUserData) ;
};

void NotificationThunks::ASNotification(CASMark *pMark, void *inUserData)
{
    CASObjectWrapper ^wrappedObj = gcnew CASObjectWrapper(pMark);
    
   //Instead of doing this I want to post mainEvent to the ASController/main thread
    ASController ^asc = (ASController^)inUserData;
    asc->mainEvent(wrappedObj);
}





ref class ASController
{....};

void ASController::mainEvent(CASObjectWrapper ^wm)
{}



请指教.非常感谢您的帮助.谢谢.



Please advise. Help is much appreciated. Thanks.

推荐答案

如果在要进行实际调用的线程上使用阻塞队列,则可以将委托实例与调用所需的数据一起发布. > 您可以使用我开发的通用队列:
用于线程通信和线程间调用的简单阻塞队列 [ ^ ].这是一个提示/技巧文章,其中包含完整的源代码和用法示例,包括如何使用线程间调用.

对于.NET Framework 4.0,这样的队列已经存在;请参阅上面引用的页面上的备用提示.

您没有解释主线程是否在运行UI.如果这是UI线程,并且您正在运行System :: Windows :: Forms或WPF UI,则应使用System::Windows::Forms::ControlSystem::Threading::DispatcherInvokeBeginInvoke方法.
请参阅我过去关于此主题的答案,以获取有关其工作方式和示例的说明(对不起,C#,但您应该了解它们;还请注意,C ++/CLI没有匿名委托):

Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5的问题 [
You can post delegate instances with the data needed to calls if on the thread you want to make the actual call you use a blocking queue.
You can use the generic queue I developed:
Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^]. This is a Tips/Trick article complete with full source code and usage samples, including the use for inter-thread invocation.

For .NET Framework 4.0 such queue already exists; see the alternative Tip on the page referenced above.

You did not explain is the main thread running UI or not. If this is UI thread and you''re running either System::Windows::Forms or WPF UI, you should use the method Invoke or BeginInvoke of System::Windows::Forms::Control or System::Threading::Dispatcher.

See my past Answer on this topic for explanation of how it works and samples (sorry, C#, but you should understand them; also note that C++/CLI does not have anonymous delegates):

Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

—SA


这篇关于将消息从不同的线程发布到主线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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