I’ve read about mutex‘s being owned by threads and only usable by the owning thread. In this answer the solution suggests that each process must take ownership of the mutex before signalling the other that is is through. I must admit my stupidity here, I don’t know how I would use events for IPC as they haven’t popped up in my reading of MSDN, I like the uniquely named mutex‘s as my solution but I don’t even know how to transfer ownership between the WindowsService and regular process. Please help.
Can I add, Jon Skeet’s tutorial here informs me that to communicate between different users, as I suppose LocalSystem to be, requires prefixing mutex names with ‘Global\’. I could not find any mention of this in the .NET documentation, I suppose he is right and must I look outside of the MSDN system more than I do.
To take ownership on already existing Mutex you can use static method Mutex.OpenExisting() with MutexRights = TakeOwnership
Considering that OpenExisting() can throw UnauthorizedAccessException exception I would suggest to create a new Mutex using following constructor:
and then by analyzing a value of the
createdNewvariable you can check whether mutext already exists or new one was created.