Using Named Mutex

ⅰ亾dé卋堺 提交于 2019-12-01 20:34:06

I had a similar issue recently.

The design of the Mutex class is a bit weird/different from the normal classes in .NET.

Using OpenMutex to check for an existing Mutex is not really nice as you have to catch an exception.

A better approach is to use the

Mutex(bool initiallyOwned, string name, out bool createdNew) 

constructor, and check the value returned by createdNew.

You don't look to check the return value from runJobWrapper anywhere - is this intentional? It is not obvious what the return value actually means anyway. Also you really shouldn't catch each any every exception that OpenExisiting could possibly throw - Out of memory? Stack overflow? etc. etc. Just catch the one you mean to handle correctly.

Also your code looks to be somewhat fragile - I wouldn't be surprised if you have race conditions.

I noticed that mutex.ReleaseMutex() was not releasing the mutex immediately..I had to call GC.Collect()

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!