释放在WPF Application.OnStartUp()中创建的命名互斥体:哪个线程拥有它? [英] Releasing a named mutex created in WPF Application.OnStartUp(): Which thread owns it?
问题描述
我在WPF应用程序的OnStartup
方法内创建一个互斥锁.互斥锁不在程序中的其他任何地方使用,其唯一目的是防止某些程序同时运行.当应用程序关闭时,如何释放此互斥锁?
I create a mutex within the OnStartup
Method of a WPF app. The mutex is not used anywhere else in the program, its only purpose is to prevent certain programs from running concurrently. How can I release this mutex when the application closes?
根据 documentation ,mutex.ReleaseMutex()
必须从创建互斥锁的同一线程中调用.但是,这带来了一个问题,因为我不控制调用OnStartup()
的线程.
According to the documentation, mutex.ReleaseMutex()
must be called from the same thread that created the mutex. However this presents a problem, since I do not control the thread that calls OnStartup()
.
假设我的OnStartup
方法看起来像这样:
Suppose my OnStartup
method looks like this:
public partial class App : Application
{
private Mutex mutex;
private bool hasHandle = false;
protected override void OnStartup(StartupEventArgs e)
{
bool createdNew;
mutex = new Mutex(false, @"Global\XYZ", out createdNew);
try
{
hasHandle = mutex.WaitOne(5000, false);
if (!hasHandle)
{/*do stuff*/};
}
catch (AbandonedMutexException)
{
hasHandle = true;
// do stuff
}
base.OnStartup(e);
}
private void releaseMutex()
{
if (mutex!=null)
{
if (hasHandle) mutex.ReleaseMutex();
mutex.Dispose();
}
}
}
保存以致电releaseMutex()
...
- 在OnExit()方法中?
protected override void OnExit(){releaseMutex();}
- 是否在ProcessExit事件处理程序中?
AppDomain.CurrentDomain.ProcessExit += (sender,e)=> releaseMutex();
- 在终结器中?
~App(){releaseMutex();}
- 在未处理的异常事件处理程序中?
AppDomain.CurrentDomain.UnhandledException += (sender,e)=> releaseMutex();
- in the OnExit() method?
protected override void OnExit(){releaseMutex();}
- in the ProcessExit event handler?
AppDomain.CurrentDomain.ProcessExit += (sender,e)=> releaseMutex();
- in a finalizer?
~App(){releaseMutex();}
- in the unhandled exception event handler?
AppDomain.CurrentDomain.UnhandledException += (sender,e)=> releaseMutex();
似乎OnExit方法有最好的机会进入相同的线程,但这似乎是一个粗略的假设.有没有办法忽略相同线程的需求?还是应该与互斥锁一起创建并存储一个单独的线程?
It seems like the OnExit method has the best chance to be in the same thread, but even that seems a sketchy assumption. Is there a way to ignore the same-thread requirement? Or should I create and store a separate thread in conjunction with my mutex?
推荐答案
我个人根本不会费心释放它,尤其是因为您处理AbandonedMutexException
.
I personally wouldn't bother releasing it at all, especially since you handle AbandonedMutexException
.
如果互斥锁不用于同步同一进程的线程,则无需显式释放它.当进程终止时,操作系统会自动关闭该进程创建的所有句柄,例如文件,套接字,互斥量,信号量和事件句柄.
If a mutex is not used to synchronize threads of the same process there is no need to explicitly release it. When a process terminates OS automatically closes all handles created by the process, such as files, sockets, mutexes, semaphores and event handles .
如果您仍然希望释放它,请考虑使用Application.OnExit()
,因为它是从主线程中调用的,就像Startup()
一样.
If you still prefer to release it consider using Application.OnExit()
since it is called from the main thread, just like the Startup()
.
这篇关于释放在WPF Application.OnStartUp()中创建的命名互斥体:哪个线程拥有它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!