两个程序进行同步操作,一个是服务(Service),一个是进程(Process),服务的运行时的用户名是System,进程运行时的用户名是启动该进程的用户。两者需要使用EventWaitHandleSecurity对象进行同步。由于是不同用户,因此实现的时候,先是用下面方法:
EventWaitHandleSecurity ewhs = new EventWaitHandleSecurity();
EventWaitHandleAccessRule evhAccessRule = new EventWaitHandleAccessRule(“Everyone”, EventWaitHandleRights.FullControl, AccessControlType.Allow);
ewhs.AddAccessRule(evhAccessRule);
handles[0] = new EventWaitHandle(false, EventResetMode.AutoReset, okeventname, out isnew, ewhs);
handles[1] = new EventWaitHandle(false, EventResetMode.AutoReset, faileventname, out isnew, ewhs);
int result = WaitHandle.WaitAny(handles, 600 * 1000);
if (result == 0)
return true;
else
return false;
但是在非英语环境下,程序会hang在 ewhs.AddAccessRule(evhAccessRule); 。
调查发现其他语言机器上用户组Everyone已经被翻译成其他语言的词汇,因此程序中hardcode进去"Everyone"是不正确的。
解决办法:
使用:
System.Security.Principal.SecurityIdentifier sid = new System.Security.Principal.SecurityIdentifier(System.Security.Principal.WellKnownSidType.WorldSid, null);
EventWaitHandleAccessRule evhAccessRule = new EventWaitHandleAccessRule(sid, EventWaitHandleRights.FullControl, AccessControlType.Allow);
代替:
EventWaitHandleAccessRule evhAccessRule = new EventWaitHandleAccessRule(“Everyone”, EventWaitHandleRights.FullControl, AccessControlType.Allow);