Click here to Skip to main content
15,894,720 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
For cache update, i want to delete the mutex if already exists.

Please let me know what is the code for deleting the mutex in C# 4.0

Thanks..
Posted
Comments
Sergey Alexandrovich Kryukov 15-Dec-15 2:57am    
First of all, are you sure you need mutex, not simple lock statement? Why?
—SA
vksvpp 15-Dec-15 3:50am    
mutex concept was suggested by my senior. datas are loaded as memorymappedfile with mutex using hashkey and that mutex and memorymappedfile is deleted if any delete operation performs
Sergey Alexandrovich Kryukov 15-Dec-15 9:53am    
Then please answer: do use your mutex between two or more different processes (not thread, processes)?
Do you understand mutual exclusion yourself? (I would not ask about it if you did not mention the "senior".)
—SA
Sinisa Hajnal 15-Dec-15 3:10am    
What have you tried?

1 solution

"Kill" is not the same as "delete", and none of the concepts are applicable to System.Threading.Mutex. Generally, CLI objects don't need deletion, because the platform is managed. But with Mutex, you need to call IDisposable.Dispose() when this object is no longer needed, and this interface method is implemented by System.Threading.WaitHandle.Dispose():
https://msdn.microsoft.com/en-us/library/dd384809%28v=vs.110%29.aspx[^].

Moreover, the only reason why this interface is implemented is that those WaitHandle classes are based on unmanaged Windows API objects. Using this interface is the usual technique of unmanaged disposing of such objects.

Also, I'm not sure you really need Mutex. A number of users with native programming experience are unaware of .NET lock mechanism: https://msdn.microsoft.com/en-us/library/c5kehkcz.aspx[^].

—SA
 
Share this answer
 
Comments
vksvpp 15-Dec-15 4:00am    
sorry.. i need delete a mutex not kill a mutex
Sergey Alexandrovich Kryukov 15-Dec-15 9:48am    
No, you don't. There is no such concept. You cannot have a need of something which does not exist. You only need to dispose the Mutex object. Besides, I'm not sure if you need Mutex at all.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900