Exemplo n.º 1
0
        private static void Main(string[] args)
        {
            try
            {
                // Upgrade the user settings if necessary.
                if (Settings.Default.SettingsUpgradeRequired)
                {
                    Settings.Default.Upgrade();
                    Settings.Default.SettingsUpgradeRequired = false;
                    Settings.Default.SaveSettings();
                }
            }
            catch (Exception ex)
            {
                Logger.Error(
                    "Error while upgrading the user settings: {0}"
                    , ex);
            }

            if (Settings.Default.FrmMainAllowOnlyOneInstance && !MainForm.CreateNamedPipe(NAMED_PIPED_NAME))
            {
                Logger.Info("Another instance of Logbert is already running.");

                // Bring the window of the other instance to front.
                NamedPipeClientStream anotherLogbertInstance = new NamedPipeClientStream(
                    "."
                    , NAMED_PIPED_NAME
                    , PipeDirection.Out);

                try
                {
                    if (!anotherLogbertInstance.ConnectAndWrite(Encoding.Default.GetBytes(BRING_TO_FRONT_MSG)))
                    {
                        Logger.Error("Unable to passing arguments to it.");
                        return;
                    }

                    Logger.Info("Passing arguments to it and exiting.");

                    if (args.Length > 0)
                    {
                        // Send the command line arguments to the other instance and exit.
                        anotherLogbertInstance = new NamedPipeClientStream(
                            "."
                            , NAMED_PIPED_NAME
                            , PipeDirection.Out);

                        anotherLogbertInstance.ConnectAndWrite(
                            Encoding.Default.GetBytes(args[0]));

                        return;
                    }
                }
                catch (Exception ex)
                {
                    Logger.Error(ex.Message);
                    return;
                }

                return;
            }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Application.Run(new MainForm(args.Length == 0
        ? string.Empty
        : args[0]));
        }
Exemplo n.º 2
0
        private static void Main(string[] args)
        {
            try {
                // Upgrade the user settings if necessary.
                if (Settings.Default.SettingsUpgradeRequired)
                {
                    Settings.Default.Upgrade();
                    Settings.Default.SettingsUpgradeRequired = false;
                    Settings.Default.SaveSettings();
                }
            }
            catch (Exception ex) {
                Logger.Error(
                    "Error while upgrading the user settings: {0}"
                    , ex);
            }


            // Commandline parameters
            CommandLineOptions cmdOptions = null;

            Parser.Default.ParseArguments <CommandLineOptions>(args)
            .WithParsed(o => cmdOptions = o)
            .WithNotParsed(errs => {
                Logger.Error("Commandline errors.");
                foreach (var e in errs)
                {
                    Logger.Error(e.ToString());
                }
            });


            if (Settings.Default.FrmMainAllowOnlyOneInstance && !MainForm.CreateNamedPipe(NAMED_PIPED_NAME))
            {
                Logger.Info("Another instance of Logbert is already running.");

                // Bring the window of the other instance to front.
                NamedPipeClientStream anotherLogbertInstance = new NamedPipeClientStream(
                    "."
                    , NAMED_PIPED_NAME
                    , PipeDirection.Out);

                try {
                    if (!anotherLogbertInstance.ConnectAndWrite(Encoding.Default.GetBytes(BRING_TO_FRONT_MSG)))
                    {
                        Logger.Error("Unable to passing arguments to it.");
                        return;
                    }

                    Logger.Info("Passing arguments to it and exiting.");

                    if (cmdOptions != null && cmdOptions.Files.Count() > 0)
                    {
                        // Send the command line arguments to the other instance and exit.
                        anotherLogbertInstance = new NamedPipeClientStream(
                            "."
                            , NAMED_PIPED_NAME
                            , PipeDirection.Out);

                        anotherLogbertInstance.ConnectAndWrite(
                            Encoding.Default.GetBytes(cmdOptions.Files.First()));

                        return;
                    }
                }
                catch (Exception ex) {
                    Logger.Error(ex.Message);
                    return;
                }

                return;
            }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Application.Run(new MainForm(cmdOptions == null || cmdOptions.Files.Count() == 0
              ? string.Empty
              : cmdOptions.Files.First()));
        }