c# - Mutex.waitOne 在特定机器上总是返回 False

标签 c# .net service mutex

我完全被难住了。我有一段依赖互斥锁来保护它的代码。当我在我的开发机器和测试机器上运行这段代码时,它工作正常。但是当我将它推送到生产机器时它失败了。

但更重要的是,它曾经有效,而且我没有对这个特定的 block 进行任何代码更改。 但等等还有更多!我有第二个进程,它使用完全相同的代码块,并且工作得很好!

互斥体声明如下:

Mutex _mutex = new Mutex(false, "SendTextMessage_11A52B63-4FC6-46DF-B72C-C45B225D4143"); 

使用它的代码块是这样的:

        public override void ForwardTextMessagesToDevice()
    {
        Trace.WriteLine("Here1");
        if (!_mutex.WaitOne(30000))
        {
            Trace.WriteLine("Here2");
            Trace.WriteLine(String.Format("return mutex ForwardTextMessage: {0} {1}", this.Name, DateTime.Now));
            return; 
        }
        try
        {
            Trace.WriteLine("Here3");
            ScheduledReports();
        }
        finally
        {
            Trace.WriteLine("Here4");
            _mutex.ReleaseMutex();
        }

跟踪输出是这样的:

Here1
Here2
return mutex ForwardTextMessage: Method 2/11/2013 3:59:06 PM
Here1
Here2
return mutex ForwardTextMessage: Method 2/11/2013 4:00:06 PM

互斥锁似乎一出门就被锁定了。我的印象是,互斥量仅在线程之间共享,并且在进程退出时被销毁,因此共享相同互斥量的两个单独的进程应该很好,对吧?另外,如果应用程序在互斥体释放之前崩溃,是否会导致此行为?

编辑:我差点忘了。之前我的应用程序随机崩溃了,我不得不强制退出它。

最佳答案

The Mutex appears to be locked immediately right out of the gate. I'm under the impression that mutex's are only shared between threads and are destroyed when the process exits so two separate processes sharing the same mutex should be good, right?

没有。 命名互斥体实例在所有进程之间共享。另一个进程可以锁定互斥锁。

摘自 Mutex constructor you are using 的文档:

Mutexes are of two types: local mutexes and named system mutexes. If you create a Mutex object using a constructor that accepts a name, it is associated with an operating-system object of that name. Named system mutexes are visible throughout the operating system, and can be used to synchronize the activities of processes.

至于你的第二个问题:

Also if the application crashed before the mutex was released could it cause this behavior?

Mutex 的文档对此进行了介绍。 :

If a thread terminates while owning a mutex, the mutex is said to be abandoned. The state of the mutex is set to signaled and the next waiting thread gets ownership. If no one owns the mutex, the state of the mutex is signaled. Beginning in version 2.0 of the .NET Framework, an AbandonedMutexException is thrown in the next thread that acquires the mutex. Prior to version 2.0 of the .NET Framework, no exception was thrown.

在应用程序终止的情况下,互斥锁应被标记为已放弃。尝试获取 Mutex 应该引发 AbandonedMutexException

关于c# - Mutex.waitOne 在特定机器上总是返回 False,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14823362/

相关文章:

c# - System.Data.dll 附加信息 : Invalid object name 中发生类型 'System.Data.SqlClient.SqlException' 的异常

c# - 在 API 代码中,对列表类型 C# 属性使用私有(private) setter 是个好主意吗?

c# - 部分方法 C# 3.0 的问题

.net - 为 Windows Forms .NET 寻找一些好的 GUI 测试框架/GUI 自动化测试工具

Python - 如何获取在我的计算机上运行的服务

Android 从 activity/asynctask 启动服务

c# - ASP.NET MVC MySQL 拒绝用户 'root' @'localhost' 访问(使用密码 : NO)

c# - 如何在不丢失小数的情况下将float转换为int?就像它在内存中显示的一样

windows - Windows 上的 Scala 服务

javascript - 返回 Bool 到 AJAX 请求