Winoke Form Invoke () in Unmanaged C ++

I played with a DataBus type for a hobby project and I had a problem. Front components should notify the user interface that something has happened. My bus implementation delivers messages synchronously with the sender. In other words, when you call the Send()method is blocked until all the handlers call. (This allows callers to use stack memory management for event objects.)

However, consider the case where the event handler updates the GUI in response to the event. If the handler is invoked and the message sender lives in a different thread, the handler cannot update the GUI due to Win32 GUI elements that have thread affinity. More dynamic platforms, such as .NET, allow you to handle this by calling the special Invoke () method to move the method call (and arguments) to the user interface stream. I assume they use a .NET parking window or similar for similar things.

There was a painful curiosity: can we do this in C ++, even if we limit the scope of the problem? Can we do this better than existing solutions? I know that Qt does something similar with a function moveToThread().

More kindly, I mentioned that I am specifically trying to avoid code of the following form:

if(! this->IsUIThread())
{
    Invoke(MainWindowPresenter::OnTracksAdded, e);
    return;
}

. WinForms . , , , -, .

:

  • DeferredFunction - , FastDelegate, . , .

  • UIEventHandler - . Execute(), . ( ), "" instance, method event. PostThreadMessage().

  • , hook "" -. , , (WTL).

, ? , , . , , , - , ? ?

+3
2

Win32, , , PostMessage Windows , , info, wParam/lParam.

, .NET Control.Invoke.

: currios, , , .

Control.Invoke MarshaledInvoke, .., RegisterWindowMessage PostMessage. , :)

+3

:

, :

  • - , , QueueUserAPC(). APC , , , , , . - . , , .

  • PostThreadMessage(), . , QueueUserAPC() , , , API , . . . , .

  • , WndProc PostMessage() . , . , , , ( ). , , .

+1

Source: https://habr.com/ru/post/1740859/


All Articles