简体   繁体   English

C#和C ++之间的线程同步

[英]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. 我有一个C#WPF应用程序,该应用程序需要调用非托管C ++ DLL并从中获取简单的事件通知。 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. DLL将等待无法在.NET中看到的键盘事件(当应用程序没有输入焦点时会显示VK_MEDIA_PLAY_PAUSE),并通知主应用程序。 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. 同样,使用命名管道,COM,套接字或p / invoke似乎超出了我的需要。

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. 具体来说,我将在WPF应用程序中启动一个工作线程,该线程将等待键盘事件被信号通知,然后通知主UI线程在发生事件后立即执行操作。

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. 在C ++方面,CreateEvent创建命名事件。 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. 但是在C#方面,首先出现的2种事件类型是AutoResetEvent和ManualResetEvent,它们不接受名称,而EventWaitHandle接受。 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. 在我的项目中,响应于所需的击键,在C ++端设置了事件,而C#端在BackgroundWorker线程中等待事件并在事件发生时对其进行操作。

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

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