C ++的Win32:执行对UI线程的方法,由于在后台线程事件 [英] c++ win32: executing a method on ui thread due to an event on background thread

查看:736
本文介绍了C ++的Win32:执行对UI线程的方法,由于在后台线程事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个后台线程是轮询服务器。结果
当有数据,我要处理的UI线程上的数据。

I've got a background thread that is polling a server.
When there's data, i want to handle the data on the ui thread.

如果我存储在主窗口的HWND。

If I store the hwnd of the main window.

我怎样才能得到一个特定的方法 -
静态无效的DataHandler(void *的数据)
要在UI线程上执行。

How can I get a particular method -- static void DataHandler(void* data) to be executed on the ui thread.

我想创建一个计时器传递HWND和函数指针会的工作。结果
但有一个更好的办法?我可以用PostMessage的以某种方式获得的DataHandler调用。

I think creating a timer passing the hwnd and the function pointer would work.
But is there a better way? Can i use postmessage to somehow get the datahandler invoked.

另外,我不写的UI code,所以我不必修改消息循环事情的能力。

Also, I'm not writing the ui code, so I don't have the ability to modify anything in the message loop.

推荐答案

有一件事你可以做的 - 使用一个线程间的信令对象也许会是布尔标志一样简单。当数据的服务器轮询线程上出现时,可以发出信号旗。你可以检查此标志在UI线程的消息循环。另外,你可以只发送UI线程的自定义窗口消息。

One thing that you could do - use an inter-thread signalling object perhaps as simple as a boolean flag. When data appears on the server polling thread, you can signal the flag. You could check for this flag in the message loop of your UI thread. Alternatively, you could just send the UI thread a custom window message.

现在,我重新阅读你的问题 - 因为你不能更改UI code,这种做法是行不通的。您可以使用WIN32 API添加自己的自定义消息钩子函数来解决这个问题。

Now that I re-read your question - since you can't change the UI code, this approach wouldn't work. You could use the WIN32 API to add your own custom message hook function to fix this problem.

这篇关于C ++的Win32:执行对UI线程的方法,由于在后台线程事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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