简体   繁体   English

从辅助线程在主线程中运行一些代码

[英]Run some code in the main thread from a worker thread

I have to signal the Main thread to run some code from one of the worker thread. 我必须发信号通知Main线程从工作线程之一运行一些代码。

That code must run in the main thread because of some COM issue( running it on the thread just fail). 由于某些COM问题,该代码必须在主线程中运行(在线程上运行它只会失败)。

I was starting to implement it with messages and event (i need to wait the code to be executed before going foward) when i remebered there was some very simple and straight way to do it in Java And SWT) 当我想到在Java和SWT中有一些非常简单直接的方法来实现它时,我就开始使用消息和事件来实现它(我需要等待代码执行才能继续前进)

  display.syncExec (new Runnable () {
  public void run () {
     if (!myWindow.isDisposed())
        myWindow.redraw ();
  }

Anything similar for MFC and windows in general? 一般而言,MFC和Windows有何相似之处?

You can use PostThreadMessage WinAPI function to send WM_USER message to main thread. 您可以使用PostThreadMessage WinAPI函数将WM_USER消息发送到主线程。 Then you can handle WM_USER in main thread and do some necessary actions. 然后,您可以在主线程中处理WM_USER并执行一些必要的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM