问题描述
我有两个Windows应用程序,一个是创造的EventWaitHandle,等待它的窗口服务。第二个应用是Windows图形用户界面它通过调用EventWaitHandle.OpenExisting(打开),并尝试设置事件。但我得到的OpenExisting异常。唯一的例外是访问路径被拒绝。
I have two windows application, one is a windows service which create EventWaitHandle and wait for it. Second application is a windows gui which open it by calling EventWaitHandle.OpenExisting() and try to Set the event. But I am getting an exception in OpenExisting. The Exception is "Access to the path is denied".
Windows服务code
windows Service code
EventWaitHandle wh = new EventWaitHandle(false, EventResetMode.AutoReset, "MyEventName");
wh.WaitOne();
Windows图形用户界面code
Windows GUI code
try
{
EventWaitHandle wh = EventWaitHandle.OpenExisting("MyEventName");
wh.Set();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
我试过同一code两个示例控制台应用程序,这是工作的罚款。
I tried the same code with two sample console application, it was working fine.
推荐答案
您需要使用的接受一个EventWaitHandleSecurity实例。例如,下面的code应该工作(这不是测试,但希望能帮助您开始):
You need to use the version of the EventWaitHandle constructor that takes an EventWaitHandleSecurity instance. For example, the following code should work (it's not tested, but hopefully will get you started):
// create a rule that allows anybody in the "Users" group to synchronise with us
var users = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
var rule = new EventWaitHandleAccessRule(users, EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify,
AccessControlType.Allow);
var security = new EventWaitHandleSecurity();
security.AddAccessRule(rule);
bool created;
var wh = new EventWaitHandle(false, EventResetMode.AutoReset, "MyEventName", out created, security);
...
另外,如果你在Vista上运行或更高版本,你需要(与全球\\,也就是preFIX名称)创建全局命名空间的事件。你也不得不如果你使用了快速用户切换功能来做到这一点在Windows XP上。
Also, if you're running on Vista or later, you need to create the event in the global namespace (that is, prefix the name with "Global\"). You'd also have to do this on Windows XP if you use the "Fast User Switching" feature.
这篇关于创建一个跨进程的EventWaitHandle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!