简体   繁体   中英

thread synchronization between c# and c++

I have a C# WPF app that needs to call an unmanaged C++ DLL and get a simple event notification from it. The DLL will wait for a keyboard event that can't be seen in .NET (VK_MEDIA_PLAY_PAUSE while the app doesn't have the input focus), and notify the main app. I've found an example for using semaphores for cross-process syncing, but that seems like overkill. Likewise, using named pipes, COM, sockets, or p/invoke seems like more than I should need.

Specifically, I'll be starting a worked thread in the WPF app that will wait for the keyboard event to be signaled, and then notify the main UI thread to do something once it has.

Thanks in advance.

Once I got pointed in the direction of Named Events, the solution became clear. On the C++ side, CreateEvent creates named events. But on the C# side, the 2 event types that come up first are AutoResetEvent and ManualResetEvent, which do not accept names, while EventWaitHandle does. Once I found that class, the rest fell into place. In my project, the event gets set on the C++ side in response to the desired key stroke, and the C# side waits for the event in a BackgroundWorker thread and acts on it when it occurs.

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