HOWTO:线程之间的消息后用升压:: ASIO? [英] HOWTO: post messages between threads with Boost::asio?

查看:80
本文介绍了HOWTO:线程之间的消息后用升压:: ASIO?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我的英语

我四处搜寻,但没有得到回答这个问题:
我有一个Windows应用程序项目,使用boost线程库。我要发帖(或调用回调)从一个工人线程的主UI线程。我学的是样品中的boost :: ASIO,都在阻塞主线程中使用他们,但我的UI线程异步工作

I've searched around, but did not get an answer to this question: I have a windows application project, using boost thread libraries. I want to post messages(or, invoke callbacks) from a worker's thread to the main UI thread. I studied the samples in boost::asio, all of them used in a blocked main thread, but my UI thread is working asynchronous.

请你帮我吗?非常感谢!

Would you please help me? thanks a lot!

推荐答案

由于UI线程都有自己的消息循环,你不能在它的上下文调用阻塞 :: io_service对象的run()功能。你可以做的是用交错 io_service对象:: poll_one轮询UI相关的方法()

Since the UI thread has its own message loop, you can't call in its context the blocking io_service::run() function. What you can do is to interleave a polling UI-related method with io_service::poll_one():

  // WARINING: untested code
  MSG msg;
  while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    if (io_service_.stopped())
      io_service_.reset();
    error_code ec;
    io_service_.poll_one(ec);
  }

现在,当您发布到函子 io_service对象_ ,你实际上它们张贴到主界面线程。

Now, when you post functors to io_service_, you actually post them to the main GUI thread.

(当然,如果你使用一些GUI框架,您应该使用合适的框架的方法,而不是的PeekMessage

(Certainly, if you use some GUI framework, you should use the appropriate framework method instead of PeekMessage.)

这篇关于HOWTO:线程之间的消息后用升压:: ASIO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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