Exemplo n.º 1
0
        protected override void OnStartup(StartupEventArgs e)
        {
            _instanceMutex = new Mutex(true, MutexName);

            if (CommandLine.Default.Updated)
            {
                if (!_instanceMutex.WaitOne(60000)) // 60s to close the previous instance
                {
                    MessageBox.Show("WindowManager was unabled to restart", "WindowManager", MessageBoxButton.OK);
                    Environment.Exit(0);
                }
            }
            else
            {
                if (!_instanceMutex.WaitOne(0))
                {
                    // Another instance is already started.
                    // Send a message to show the main window
                    try
                    {
                        IpcChannel channel = new IpcChannel("Client");
                        ChannelServices.RegisterChannel(channel, false);
                        SingleInstance app =
                            (SingleInstance)
                            Activator.GetObject(typeof(SingleInstance), string.Format("ipc://{0}/RemotingServer", Name));

                        if (e.Args.Length == 0)
                        {
                            app.Execute(null);
                        }
                        else
                        {
                            app.Execute(e.Args);
                        }
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine(ex.ToString());
                    }

                    Environment.Exit(0);
                }
            }

            try
            {
                RegisterIpcServer();
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.ToString());
            }
            base.OnStartup(e);
        }
Exemplo n.º 2
0
 static void Main(string[] args)
 {
     using (var mutex = new Mutex(false, AppId))
     {
         if (!mutex.WaitOne(0))
         {
             // 2nd instance
             // Send the command line to the first instance
             IpcChannel clientChannel = new IpcChannel();
             ChannelServices.RegisterChannel(clientChannel, false);
             SingleInstance app = (SingleInstance)Activator.GetObject(typeof(SingleInstance), string.Format("ipc://{0}/RemotingServer", AppId));
             app.Execute(args);
             return;
         }
         // 1st instance
         // Register the IPC server
         IpcChannel channel = new IpcChannel(AppId);
         ChannelServices.RegisterChannel(channel, false);
         RemotingConfiguration.RegisterWellKnownServiceType(typeof(SingleInstance), "RemotingServer", WellKnownObjectMode.Singleton);
         // Start the application
         Application.EnableVisualStyles();
         Application.SetCompatibleTextRenderingDefault(false);
         Application.Run(new Form1());
     }
 }