I have two applications, a WinForms app and a Windows Service that will both run on the same machine. I want the WinForms app to reliably detect when the service is running. I have complete control over the design and implementation of both applications.
My first thought is to use a Mutex, instantiated by the Service and detectable by the WinForms App.
Is there a better design?
Mutex is the way to go. It’s a lot less fragile than using process names etc.
However, you need to make sure the Mutex isn’t garbage collected. In the case of a service (which is event driven rather than having a “main” method which runs to completion), the most sensible way of doing this is probably to put it in a static variable.
Dispose of the mutex when the service stops, so you don’t need to wait for finalization or anything like that.