简体   繁体   English

如何将任务委派给主线程并等待其执行完成?

[英]How to delegate a task to main thread and wait for it's execution to complete?

I have a Borland C++ project where I see a synchronize() method which a worker thread can use to delegate a task to main thread and wait for task to complete. 我有一个Borland C ++项目,在这里我看到一个syncnize()方法,工作线程可以使用该方法将任务委派给主线程并等待任务完成。 In C#, there is a similar Control.Invoke() method. 在C#中,有一个类似的Control.Invoke()方法。 Is there anything similar while working in C++ in Visual Studio for both GUI and Console applications? 在Visual Studio中的C ++中为GUI和控制台应用程序工作时,是否有类似的东西? SendMessage() comes to my mind but is that equivalent of above two? 我想到了SendMessage(),但是否等同于上述两个?

SendMessage is OK for GUI applications, where main thread has a message queue. 对于GUI应用程序,在主线程具有消息队列的情况下,SendMessage是可以的。 For Console applications there is no generic way. 对于控制台应用程序,没有通用方法。 You need to decide first, how main thread should handle such requests. 您需要首先决定主线程应如何处理此类请求。 If main thread of the Console application has message queue, SendMessage is OK as well. 如果控制台应用程序的主线程具有消息队列,则SendMessage也可以。 You can think about another ways, for example, using events, everytning depends on the main application thread behavior and its ability to handle requests from another threads. 您可以考虑其他方式,例如,使用事件,每事件取决于主应用程序线程的行为及其处理来自其他线程的请求的能力。

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

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