public static void MakeSingleInstance(string name, ISingleInstance app) { EventWaitHandle eventWaitHandle = null; var isFirstInstance = false; var eventName = Environment.MachineName + "-" + name; try { eventWaitHandle = EventWaitHandle.OpenExisting(eventName); } catch { // it's the first instance isFirstInstance = true; } if (isFirstInstance) { eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, eventName); ThreadPool.RegisterWaitForSingleObject(eventWaitHandle, _OnWaitOrTimerCallback, app, Timeout.Infinite, false); // not need anymore eventWaitHandle.Close(); } else { // inform the first instance, that a new instanc has tried to start. eventWaitHandle.Set(); Environment.Exit(0); } }
private static void CreateRemoteService(ISingleInstance instance, string channelName) { messageBus = new TinyMessageBus(channelName); messageBus.MessageReceived += (_, e) => { instance.OnInstanceInvoked(e.Message.Deserialize <string[]>()); }; }
/// <summary> /// Notifies the main instance that this instance is attempting to start up. /// </summary> /// <param name="uid">The application's unique identifier.</param> public static void NotifyFirstInstance(string uid) { //create channel with first instance interface using (ChannelFactory <ISingleInstance> factory = new ChannelFactory <ISingleInstance>( new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/7thHeavenApp/" + uid))) { ISingleInstance singleInstanceInterface = factory.CreateChannel(); //pass the command-line args to the first interface singleInstanceInterface.PassStartupArgs(Environment.GetCommandLineArgs()); } }
/// <summary> /// Notifies the main instance that this instance is attempting to start up. /// </summary> /// <param name="guid">The application's unique identifier.</param> private static void NotifyMainInstance(Guid guid) { NetNamedPipeBinding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None); EndpointAddress remoteAddress = new EndpointAddress(CreateAddress(guid)); using (ChannelFactory <ISingleInstance> factory = new ChannelFactory <ISingleInstance>(binding, remoteAddress)) { ISingleInstance singleInstance = factory.CreateChannel(); singleInstance.NotifyMainInstance(Environment.GetCommandLineArgs()); } }
private void RetryIfFailToStart() { try { _delegator = _installer.Start().Single(); } catch (Exception e) { Console.WriteLine(e); _delegator = _installer.Start().Single(); } }
public StandAlone() { try { _installer.Install(); _delegator = _installer.Start().Single(); } catch { try { Dispose(); } catch { /*Do nothing*/ } throw; } NewBoltDriver(); }
public StandAlone(Pkcs12Store store) { try { _installer.Install(); UpdateCertificate(store); _delegator = _installer.Start().Single(); } catch { try { Dispose(); } catch { /*Do nothing*/ } throw; } NewBoltDriver(); }
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 SingleInstanceProxy(ISingleInstance singleInstance) { this.SingleInstance = singleInstance; }