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开发者_如何学C() 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.
精彩评论