您需要为您的事件添加权限,否则即使它是全局的,其他应用程序也无法访问它。
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 和同名的 Event 时,我只是做了一些测试。
System.Threading.WaitHandleCannotBeOpenedException = {"无法创建系统范围名称为 'Global\AE66918ED73040bbB59F2BE9405FDCA2-5501' 的 WaitHandle。不同类型的 WaitHandle 可能具有相同的名称。"}
我想这不是问题,也许你的服务正在悄悄地得到这个异常?