我正在使用互斥来同步两个进程。 Mutex正在通过服务流程创建。 但是当客户端尝试访问该互斥锁时,他会获得Unauthorizedaccessexception。 用户帐户已创建全局对象权限 这种情况发生在少数运行Windows 7的机器上,但在其他Windows 7机器上无法重现。 可能是什么原因。 谢谢你的帮助
以下是创建全局互斥锁的代码 bool gCreated; Mutex syncMutex = new Mutex(true,“Global \ SWDBOject”,out gCreated); var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid,null), MutexRights.FullControl,AccessControlType.Allow); var securitySettings = new MutexSecurity(); securitySettings.AddAccessRule(allowEveryoneRule); syncMutex.SetAccessControl(securitySettings);
答案 0 :(得分:1)
您需要为您的活动添加权利,否则其他应用程序无法访问它,即使它是全局的。
Private Shared Function CreateEvent(name As String) As _
System.Threading.EventWaitHandle
Dim created As Boolean
Dim users As New SecurityIdentifier(WellKnownSidType.WorldSid, Nothing)
Dim rule As New EventWaitHandleAccessRule(users, _
EventWaitHandleRights.FullControl, _
AccessControlType.Allow)
Dim security As New EventWaitHandleSecurity()
security.AddAccessRule(rule)
Try
Dim theHandle As New System.Threading.EventWaitHandle(False, _
Threading.EventResetMode.AutoReset, name, _
created, security)
If created Then
Return theHandle
Else
End If
Catch ex As UnauthorizedAccessException
'there was another instance created in this very small window.
'therefore just attach to the existing.
Catch ex As Exception
'do something with this.
End Try
Return Nothing
End Function
在您使用的用户代码中
System.Threading.Mutex.OpenExisting
而不是尝试创建新的。
我能想到的唯一另一件事是你看到的全局对象,因为这个名称相当通用,它不是你创建的对象吗?
当我创建Mutex和具有相同名称的事件时,我刚做了一些测试。
System.Threading.WaitHandleCannotBeOpenedException = {“无法创建具有系统范围名称'Global \ AE66918ED73040bbB59F2BE9405FDCA2-5501'的WaitHandle。不同类型的WaitHandle可能具有相同的名称。”}
我想这不是问题,也许你的服务正在悄悄得到这个例外?