Releasing a named mutex created in WPF Application.OnStartUp(): Which thread owns it?

前端 未结 2 1253
隐瞒了意图╮
隐瞒了意图╮ 2021-01-21 23:03

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

2条回答
  •  天涯浪人
    2021-01-21 23:34

    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 .

    If you still prefer to release it consider using Application.OnExit() since it is called from the main thread, just like the Startup().

提交回复
热议问题