I have a situation where a service is running a set of processes in the background. Those processes need to be started/stopped from a GUI application on demand. The issue is that the list of running processes is held in memory on an object that is designed to monitor and control them. This object is on the services instance and so not accessible from the GUI applications instance. What I think I need is to turn that object into a Singleton that is Globally static on the machine, like a Mutex, so that I can run it's methods from the GUI and have them affect the service. Example code is below, this is not the code I have but a much simplified version of it. I am interested in A solution to the Mutex based Singleton or an alternative that would suit my needs better.
namespace SystemWideSingleton
{
class Program
{
static void Main(string[] args)
{
while (true)
{
Console.Write("Count = " + GlobalSingleton.Instance.Count++);
Console.ReadLine();
}
}
}
public class GlobalSingleton
{
static GlobalSingleton() { }
private GlobalSingleton()
{
Count = 0;
}
private static readonly GlobalSingleton _instance = new GlobalSingleton();
public static GlobalSingleton Instance { get { return _instance; } }
public int Count { get; set; }
}
}
I would expect the above code to run as two console applications and for each application to share the output. ex.
Application 1: Application 2:
-------------- --------------
0 0
1 3
2 4
5 7
6 8
Any help would be very much appreciated.
P.S. I know that right now all I get out of the singleton is that it will work between threads on the same application instance. I need it to work across instances on the machine.