命名的系统互斥体无法识别无法识别、互斥、系统

2023-09-03 02:55:07 作者:云朵有点甜

我想命名的系统互斥体的方法来同步两个过程 -

在一个C#窗口服务 在桌面C#应用程序

在创建互斥量,未创建互斥过程似乎并没有检测出存在的互斥锁。下面更详细地:

Windows服务负责创建互斥体(无prefixes全局/本地等只是一个正常命名的系统互斥体)如下:

 互斥myMutex = NULL;
        尝试
        {
            myMutex = Mutex.OpenExisting(myMutexName);

        }
        赶上(WaitHandleCannotBeOpenedException X)
        {
            //不存在。
            尝试
            {
                this.GetLogger()信息(创建互斥量)。
                布尔createdNew = FALSE;
                新的互斥(假,myMutexName,出createdNew);
                如果(!createdNew)抛出新的异常(无法创建互斥量);

            }
            赶上(例外前)
            {
                this.Stop();
            }

        }
        赶上(例外X)
        {
            this.Stop();
        }
        最后
        {

            如果(myMutex!= NULL)
            {
                尝试
                {
                    myMutex.ReleaseMutex();
                }
                赶上(例外X)
                {
                    this.GetLogger()警告(无法释放互斥)。
                }
            }
        }
 

我有这样的ONSTART()。此外,在其他地方服务同步我对这个互斥。

电脑 不能识别网络 是怎么回事

现在,在应用程序,我用它是这样的:

 互斥myMutex = NULL;

        尝试
        {
            myMutex = Mutex.OpenExisting(this.myMutexName);
            myMutex.WaitOne();
//做需要的东西


        }
        赶上(WaitHandleCannotBeOpenedException wcbox)
        {
            //还不存在。服务没有运行?
            。this.GetLogger()错误(后台服务没有运行);
            shutdownApp();
        }
        最后
        {
            如果(myMutex!= NULL)
            {
                尝试
                {
                    myMutex.ReleaseMutex();
                }
                赶上(例外X)
                {
                }
            }
        }
 

我的问题:

通过服务正在运行,应用程序将引发WaitHandleCannotBeOpenedException,这意味着它不会看到它存在互斥(我用的工具WinObj验证了这一点,在BaseNamedObjects类型突变存在互斥)。为什么?我运行在同一帐户的服务为我登录。即使没有,这不应该成为问题,因为命名的互斥体是操作系统的广泛对象吧​​?

我不是当互斥量将被摧毁完全清楚。目前,我看到,当我启动该服务的互斥体被创建,当我停止服务,我看到了互斥不再存在(WinObj)。这是否意味着互斥垃圾收集时,有没有人在等待它,当创始人过程结束?

在一般情况下,什么是命名的系统互斥体的生命周期。特别是,当它死吗?

解决方案

既然你要共享跨会话互斥体,你必须使用全球命名空间,或创建您自己的私人空间。

既然这样,你是不是指定一个命名空间。而同时该服务默认使用全局命名空间,交互式应用程序将使用本地命名空间。

I am trying named system mutex approach to synchronize two processes-

a c# windows service a desktop c# app

When the mutex is created, process that didn't create the mutex doesn't seem to detect the existing mutex. Below in more detail:

Windows service is responsible for creating the mutex(No prefixes-Global/Local etc. Just a normal named system mutex) as follows:

        Mutex myMutex= null;
        try
        {
            myMutex= Mutex.OpenExisting(myMutexName);

        }
        catch (WaitHandleCannotBeOpenedException x)
        {
            //doesn't exist. 
            try
            {
                this.GetLogger().Info("Create Mutex");
                bool createdNew = false;
                new Mutex(false, myMutexName, out createdNew);
                if (!createdNew) throw new Exception("Unable to create Mutex");

            }
            catch (Exception ex)
            {
                this.Stop();
            }

        }
        catch (Exception x)
        {
            this.Stop();
        }
        finally
        {

            if (myMutex!= null)
            {
                try
                {
                    myMutex.ReleaseMutex();
                }
                catch(Exception x)
                {
                    this.GetLogger().Warn("Unable to release mutex");
                }
            }
        }

I have this in onStart(). Also, elsewhere in the service I synchronize on this mutex.

Now, in the application I use it thus:

            Mutex myMutex= null;

        try
        {
            myMutex= Mutex.OpenExisting(this.myMutexName);
            myMutex.WaitOne();
//do required stuff


        }
        catch (WaitHandleCannotBeOpenedException wcbox)
        {
            //doesn't exist yet. service not running??
            this.GetLogger().Error("Background Service is not Running");
            shutdownApp();
        }
        finally
        {
            if (myMutex!= null)
            {
                try
                {
                    myMutex.ReleaseMutex();
                }
                catch (Exception x)
                {
                }
            }
        }

My issues:

With service is running, the app throws WaitHandleCannotBeOpenedException which means it doesn't see the mutex which exists (I verified this using the tool "WinObj", mutex exists under "BaseNamedObjects" as type "Mutant"). Why? I run the service under same account as I am logged in. Even without, that shouldn't be the issue because named mutex is operating system wide object right?

I am not exactly clear on when the mutex will get destroyed. Currently, I see that the mutex gets created when I start the service and when I stop the service, I see the mutex no longer exists(WinObj). Does that mean mutex is garbage collected when there is no one waiting on it and when the creator process ends?

In general, what is the life cycle of a named system mutex. Specifically, when does it die?

解决方案

Since you want to share a mutex across sessions you must use either the Global namespace, or create your own private namespace.

As it stands, you are not specifying a namespace. And whilst the service defaults to use the global namespace, your interactive application will be using the local namespace.