Creating a Cross-Process EventWaitHandle

前端 未结 2 1263
日久生厌
日久生厌 2020-12-08 04:56

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 EventWait

2条回答
  •  旧巷少年郎
    2020-12-08 05:53

    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);
    ...
    

    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.

提交回复
热议问题