示例#1
0
 static void Main(string[] Args)
 {
     Application.EnableVisualStyles();
     Application.SetCompatibleTextRenderingDefault(false);
     Assembly.Load("Actions");
     if (SingleApplicationInstance.NoPreviousInstance("apsimrun", Args))
     {
         try
         {
             SimulationRunnerForm MainForm = new SimulationRunnerForm(Args);
             Application.Run(MainForm);
         }
         catch (Exception err)
         {
             if (err.InnerException == null)
             {
                 MessageBox.Show(err.Message);
             }
             else
             {
                 MessageBox.Show(err.InnerException.Message);
             }
         }
     }
 }
 protected override void WndProc(ref Message m)
 {
     // We need to override the windows message proc to intercept our WM_COPYDATA message. This is
     // sent when a second instance of ApsimRun is created with a command line argument. The lpData
     // field of that WM_COPYDATA structure will contain the command line.
     if (m.Msg == SingleApplicationInstance.WM_COPYDATA)
     {
         // Comes through here when an instance of ApsimRun was already in memory.
         // Reusing the same instance.
         string[] files = { SingleApplicationInstance.ProcessWM_COPYDATA(m) };
         Activate();
         RunSimulations(files);
     }
     base.WndProc(ref m);
 }
        public void Invoke()
        {
            var instance1 = FirstInstance;
            Assert.IsTrue(instance1.IsFirstInstance);

            instance1.FirstInstance = new TestApplication();
            Assert.IsNotNull(instance1.FirstInstance);
            Assert.AreEqual(0, instance1.FirstInstance.InvokeCounter);
            instance1.FirstInstance.Invoke();
            Assert.AreEqual(1, instance1.FirstInstance.InvokeCounter);

            using (var instance2 = new SingleApplicationInstance<TestApplication>("test")) {
                Assert.IsFalse(instance2.IsFirstInstance);

                Assert.IsNotNull(instance2.FirstInstance);
                Assert.AreEqual(1, instance2.FirstInstance.InvokeCounter);
                instance2.FirstInstance.Invoke();
                Assert.AreEqual(2, instance2.FirstInstance.InvokeCounter);
            }
        }
        public void Dispose()
        {
            FirstInstance.Dispose();
            FirstInstance = null;

            try {
                var mutex = Mutex.OpenExisting("test");
                Assert.Fail();
            } catch (WaitHandleCannotBeOpenedException) {
            }
            Assert.IsNull(ChannelServices.GetChannel("ipc"));

            var appData = Environment.GetFolderPath(
                Environment.SpecialFolder.LocalApplicationData
            );
            var lockDirectory = Path.Combine(appData, "SingleApplicationInstance");
            var lockFile = Path.Combine(lockDirectory, "test");
            //Assert.IsFalse(File.Exists(lockFile));

            FirstInstance = new SingleApplicationInstance<TestApplication>("test");
            Assert.IsTrue(FirstInstance.IsFirstInstance);
            //Assert.IsTrue(File.Exists(lockFile));
        }
        public void IsFirstInstance()
        {
            var instance1 = FirstInstance;
            Assert.IsTrue(instance1.IsFirstInstance);

            using (var instance2 = new SingleApplicationInstance<TestApplication>("test")) {
                Assert.IsFalse(instance2.IsFirstInstance);
            }
        }
 public void SetUp()
 {
     FirstInstance = new SingleApplicationInstance<TestApplication>("test");
 }
示例#7
0
        public static void Main(string[] args)
        {
            var debug       = false;
            var link        = String.Empty;
            var engine      = String.Empty;
            var newInstance = false;
            var options     = new OptionSet();

            options.Add(
                "d|debug",
                _("Enable debug output"),
                v => {
                debug = true;
            }
                );
            options.Add(
                "h|help",
                _("Show this help"),
                v => {
                Console.WriteLine("Usage: smuxi-frontend-gnome [options]");
                Console.WriteLine();
                Console.WriteLine(_("Options:"));
                options.WriteOptionDescriptions(Console.Out);
                Environment.Exit(0);
            }
                );
            options.Add(
                "e|engine=",
                _("Connect to engine"),
                v => {
                engine = v;
            }
                );
            options.Add(
                "open|open-link=",
                _("Opens the specified link in Smuxi"),
                v => {
                link = v;
            }
                );
            options.Add(
                "new-instance",
                _("Starts a new Smuxi instance and ignores an existing one"),
                v => {
                newInstance = true;
            }
                );

            try {
                options.Parse(args);

#if LOG4NET
                // initialize log level
                log4net.Repository.ILoggerRepository repo = log4net.LogManager.GetRepository();
                if (debug)
                {
                    repo.Threshold = log4net.Core.Level.Debug;
                }
                else
                {
                    repo.Threshold = log4net.Core.Level.Info;
                }
#endif

                try {
                    Instance = new SingleApplicationInstance <CommandLineInterface>();
                    if (Instance.IsFirstInstance)
                    {
                        Instance.FirstInstance = new CommandLineInterface();
                        if (!String.IsNullOrEmpty(link))
                        {
                            Instance.FirstInstance.OpenLink(link);
                        }
                    }
                    else
                    {
                        if (!String.IsNullOrEmpty(link))
                        {
                            var msg = _("Passing link to already running Smuxi instance...");
#if LOG4NET
                            _Logger.Info(msg);
#else
                            Console.WriteLine(msg);
#endif
                            Instance.FirstInstance.OpenLink(link);
                        }
                        else if (!newInstance)
                        {
                            var msg = _("Bringing already running Smuxi instance to foreground...");
#if LOG4NET
                            _Logger.Info(msg);
#else
                            Console.WriteLine(msg);
#endif
                            Instance.FirstInstance.PresentMainWindow();
                        }

                        if (!newInstance)
                        {
                            // don't initialize/spawn another instance
                            return;
                        }
                    }
                } catch (Exception ex) {
#if LOG4NET
                    _Logger.Warn("Single application instance error, ignoring...", ex);
#endif
                }

                Frontend.Init(args, engine);
            } catch (Exception e) {
#if LOG4NET
                _Logger.Fatal(e);
#endif
                // when Gtk# receives an exception it is not usable/relyable anymore!
                // except the exception was thrown in Frontend.Init() itself
                if (Frontend.IsGtkInitialized && !Frontend.InGtkApplicationRun)
                {
                    Frontend.ShowException(e);
                }

                // rethrow the exception for console output
                throw;
            }
        }
示例#8
0
文件: Main.cs 项目: pacificIT/smuxi
        public static void Main(string[] args)
        {
            var debug = false;
            var link = String.Empty;
            var engine = String.Empty;
            var newInstance = false;
            var options = new OptionSet();
            options.Add(
                "d|debug",
                _("Enable debug output"),
                v => {
                    debug = true;
                }
            );
            options.Add(
                "h|help",
                _("Show this help"),
                v => {
                    Console.WriteLine("Usage: smuxi-frontend-gnome [options]");
                    Console.WriteLine();
                    Console.WriteLine(_("Options:"));
                    options.WriteOptionDescriptions(Console.Out);
                    Environment.Exit(0);
                }
            );
            options.Add(
                "e|engine=",
                _("Connect to engine"),
                v => {
                    engine = v;
                }
            );
            options.Add(
                "open|open-link=",
                _("Opens the specified link in Smuxi"),
                v => {
                    link = v;
                }
            );
            options.Add(
                "new-instance",
                _("Starts a new Smuxi instance and ignores an existing one"),
                v => {
                    newInstance = true;
                }
            );

            try {
                options.Parse(args);

            #if LOG4NET
                // initialize log level
                log4net.Repository.ILoggerRepository repo = log4net.LogManager.GetRepository();
                if (debug) {
                    repo.Threshold = log4net.Core.Level.Debug;
                } else {
                    repo.Threshold = log4net.Core.Level.Info;
                }
            #endif

                try {
                    Instance = new SingleApplicationInstance<CommandLineInterface>();
                    if (Instance.IsFirstInstance) {
                        Instance.FirstInstance = new CommandLineInterface();
                        if (!String.IsNullOrEmpty(link)) {
                            Instance.FirstInstance.OpenLink(link);
                        }
                    } else {
                        if (!String.IsNullOrEmpty(link)) {
                            var msg = _("Passing link to already running Smuxi instance...");
            #if LOG4NET
                            _Logger.Info(msg);
            #else
                            Console.WriteLine(msg);
            #endif
                            Instance.FirstInstance.OpenLink(link);
                        } else if (!newInstance) {
                            var msg = _("Bringing already running Smuxi instance to foreground...");
            #if LOG4NET
                            _Logger.Info(msg);
            #else
                            Console.WriteLine(msg);
            #endif
                            Instance.FirstInstance.PresentMainWindow();
                        }

                        if (!newInstance) {
                            // don't initialize/spawn another instance
                            return;
                        }
                    }
                } catch (Exception ex) {
            #if LOG4NET
                    _Logger.Warn("Single application instance error, ignoring...", ex);
            #endif
                }

                Frontend.Init(args, engine);
            } catch (Exception e) {
            #if LOG4NET
                _Logger.Fatal(e);
            #endif
                // when Gtk# receives an exception it is not usable/relyable anymore!
                // except the exception was thrown in Frontend.Init() itself
                if (Frontend.IsGtkInitialized && !Frontend.InGtkApplicationRun) {
                    Frontend.ShowException(e);
                }

                // rethrow the exception for console output
                throw;
            }
        }