简体   繁体   中英

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. In C#, there is a similar Control.Invoke() method. Is there anything similar while working in C++ in Visual Studio for both GUI and Console applications? SendMessage() comes to my mind but is that equivalent of above two?

SendMessage is OK for GUI applications, where main thread has a message queue. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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