AbandonedMutexException系统互斥没有抓到2日当运行运行第一次杀害 [英] AbandonedMutexException for system mutex not caught on 2nd run when 1st run killed

查看:109
本文介绍了AbandonedMutexException系统互斥没有抓到2日当运行运行第一次杀害的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了以下测试程序:

I have created the following test program:

static void Main(string[] args)
{
    using (var mutex = new Mutex(false, "foobar"))
    {
        Console.WriteLine("Created mutex");
        try
        {
            try
            {
                if (!mutex.WaitOne(TimeSpan.FromSeconds(5), false))
                {
                    Console.WriteLine("Unable to acquire mutex");
                    Environment.Exit(0);
                }
            }
            catch (AbandonedMutexException)
            {
                Console.WriteLine("Mutex was abandoned");
            }

            Console.WriteLine("Acquired mutex - sleeping 10 seconds");
            Thread.Sleep(10000);
        }
        finally
        {
            mutex.ReleaseMutex();
            Console.WriteLine("Released mutex");
        }
    }



的想法是,我运行的程序,而线程处于睡眠状态,持续10秒,我杀通过任务管理器的进程。下一次,我跑的过程中,我期待了 AbandonedMutexException 将在 WaitOne的()通话抓获。但我的不可以看到输出的互斥体被抛弃了。

The idea is that I run the program, and while the thread is sleeping for 10 seconds, I kill the process via task manager. Next time I run the process, I'm expecting that the AbandonedMutexException would be caught on the WaitOne() call. But I'm not seeing the output "Mutex was abandoned".

MSDN文档提到了以下内容:

当一个线程放弃一个互斥体,异常被抛出在获取该互斥锁的下一个
线。

When a thread abandons a mutex, the exception is thrown in the next thread that acquires the mutex.

不过,它看起来像操作系统发布时,我的进程被终止(而不是在同一应用程序中的另一个线程)互斥。

However, it looks like the OS is releasing the mutex when my process is killed (rather than another thread within the same application).

有没有办法对我来说,能够检测到这种方式放弃了互斥?

Is there a way for me to be able to detect a mutex abandoned in this manner?

推荐答案

什么你观察是正确的行为。 从文档

What you're observing is the correct behavior. From the documentation:

命名的互斥体是一个系统对象,其生存期由表示它的Mutex对象的寿命限制。当第一个进程创建其互斥对象被创建的命名互斥;在这个例子中,名为互斥通过运行该程序的第一个进程拥有。 当代表它所有的互斥对象已被释放命名的互斥体被破坏

The named mutex is a system object whose lifetime is bounded by the lifetimes of the Mutex objects that represent it. The named mutex is created when the first process creates its Mutex object; in this example, the named mutex is owned by the first process that runs the program. The named mutex is destroyed when all the Mutex objects that represent it have been released.

如果你运行您同时在两个独立的进程(可能增加超时),并杀死的第一个处理程序,您的可以的观察你的预期行为。当第一个进程被终止,它放弃了互斥,这使得第二个进程,以获得互斥体,此时它立即引发AbandonedMutexException。

If you run your program in two separate processes simultaneously (perhaps increasing the timeouts) and kill the first process, you can observe your expected behavior. When the first process is killed, it abandons the Mutex, which allows the second process to obtain the mutex, at which point it immediately throws an AbandonedMutexException.

如果你想确定你的进程是否正常退出,则需要不同的机制。可以做到这一点许多方式,但最简单的是将有进程创建在启动时在一个已知的位置中的文件和优美的清理后删除该文件。然后,您可以检查该文件,如果发现该文件(和你的程序没有运行实例),你知道上次关机不正常或没有成功完成。

If you want to determine whether your process exited gracefully, you need a different mechanism. You could accomplish this a number of ways, but one of the simplest would be to have the process create a file in a known location at startup time and delete that file after a graceful cleanup. Then you can check for that file, and if you find the file (and no running instance of your program), you know the last shutdown was not graceful or was not completed successfully.

这篇关于AbandonedMutexException系统互斥没有抓到2日当运行运行第一次杀害的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆