且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Openexisting全局互斥体上的UnauthorizedAccessException

更新时间:2023-11-13 22:50:04

您将需要为事件添加权限,否则即使是全局的,其他应用程序也无法访问它.

You will need to add Rights to your event otherwise the other application cannot access it, even though it's global.

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 ,而不是尝试创建一个新的.

In your user code are you using System.Threading.Mutex.OpenExisting rather than attempting to Create a new one.

我能想到的唯一另一件事是您看到的全局对象(因为该名称相当通用)是不是您创建的对象?

The only other thing I can think of is that the Global Object you are seeing because that name is fairly generic is that it's not the object that you created?

当我创建Mutex和具有相同名称的事件时,我刚刚进行了一些测试.

I just did some tests when I create Mutex and an Event with same name.

System.Threading.WaitHandleCannotBeOpenedException = {无法创建系统范围名称为'Global \ AE66918ED73040bbB59F2BE9405FDCA2-5501'的WaitHandle.不同类型的WaitHandle可能具有相同的名称."}

System.Threading.WaitHandleCannotBeOpenedException = {"A WaitHandle with system-wide name 'Global\AE66918ED73040bbB59F2BE9405FDCA2-5501' cannot be created. A WaitHandle of a different type might have the same name."}

我想这不是问题所在,也许您的服务正在悄悄地获得此异常??

I guess that's not the issue, maybe your service is getting this exception quietly??