public SingleInstanceHandler(ISingleInstance singleInstance) { string mutexName = "Sfn.UnpackQueue.SingleInstance.Mutex"; string proxyObjectName = "SingleInstanceProxy"; bool firstInstance = false; singleInstanceMutex = new Mutex(true, mutexName, out firstInstance); // Register Ipc server if (firstInstance) { // Create a new Ipc server channel ipcChannel = new IpcServerChannel(mutexName); // Register the server channel with Windows ChannelServices.RegisterChannel(ipcChannel, false); // Register the proxy type as a singleton RemotingConfiguration.RegisterWellKnownServiceType( typeof(SingleInstanceProxy), proxyObjectName, WellKnownObjectMode.Singleton); // Create the one and only proxy object to be used. proxy = new SingleInstanceProxy(singleInstance); // Publish the proxy object so that clients can access it. RemotingServices.Marshal(proxy, proxyObjectName); // Flag this instance as the master IsMaster = true; } // Register Ipc client else { // Create the uri to the master proxy object string proxyUri = string.Format("ipc://{0}/{1}", mutexName, proxyObjectName); // Create client channel and register it ipcChannel = new IpcClientChannel(); ChannelServices.RegisterChannel(ipcChannel, false); // Fetch the object using reflection proxy = (SingleInstanceProxy)Activator.GetObject(typeof(SingleInstanceProxy), proxyUri); // This is not the master IsMaster = false; } }
public SingleInstanceHandler(ISingleInstance singleInstance) { string mutexName = "Sfn.UnpackQueue.SingleInstance.Mutex"; string proxyObjectName = "SingleInstanceProxy"; bool firstInstance = false; singleInstanceMutex = new Mutex(true, mutexName, out firstInstance); // Register Ipc server if (firstInstance) { // Create a new Ipc server channel ipcChannel = new IpcServerChannel(mutexName); // Register the server channel with Windows ChannelServices.RegisterChannel(ipcChannel, false); // Register the proxy type as a singleton RemotingConfiguration.RegisterWellKnownServiceType( typeof(SingleInstanceProxy), proxyObjectName, WellKnownObjectMode.Singleton); // Create the one and only proxy object to be used. proxy = new SingleInstanceProxy(singleInstance); // Publish the proxy object so that clients can access it. RemotingServices.Marshal(proxy, proxyObjectName); // Flag this instance as the master IsMaster = true; } // Register Ipc client else { // Create the uri to the master proxy object string proxyUri = string.Format("ipc://{0}/{1}", mutexName, proxyObjectName); // Create client channel and register it ipcChannel = new IpcClientChannel(); ChannelServices.RegisterChannel(ipcChannel, false); // Fetch the object using reflection proxy = (SingleInstanceProxy)Activator.GetObject(typeof(SingleInstanceProxy), proxyUri); // This is not the master IsMaster = false; } }