求购:不从AbandonedMutexException遭受跨进程同步不从、AbandonedMutexException

2023-09-03 05:59:59 作者:一曲诀别诗

我有获得互斥体,然后终止多个线程。

I have several threads that acquire Mutexes and then terminate.

的互斥被存储在主资源库,而当该程序存在被正确释放。然而,当分配一个互斥体的线程存在,互斥锁被自动释放,以及随后的获取AbandonedMutexException(也可根据的文档)。

The mutexes are stored in a main repository, and are properly released when the program exists. However, when the thread that allocated a Mutex exists, the mutex gets released automatically, and subsequent acquire AbandonedMutexException (also according to the documentation).

我怎样才能避免这种情况的异常,并继续使用分配线程完成,即使一个互斥?难道还有其他的,更合适的同步结构在.net不具有此限制。

How can I avoid this exception, and keep using a Mutex even after the allocating thread finished? Is there another, more appropriate synchronization construct in .Net that doesn't have this limitation.

注意 - 我在寻找类似的语义互斥一个跨进程同步机制

Note - I am looking for a cross-process synch mechanism with similar semantics to Mutex.

推荐答案

看起来的EventWaitHandle我想要做什么。它有一个构造函数的名称,所以它的完美的跨进程同步,它不存在这个问题。

Looks like EventWaitHandle does what I want. It has a constructor that takes a name, so it's perfect for cross-process synch, and it doesn't have this problem.