Esempio n. 1
0
 /// <summary>
 /// Broadcasts the InitInstance event.
 /// </summary>
 /// <param name="sender">The sender of the event.</param>
 /// <param name="e">Event arguments.</param>
 /// <returns>True if the MainForm object should be shown.</returns>
 private void OnInitInstance(object sender, InitInstanceEventArgs e)
 {
     if (InitInstance != null)
     {
         InitInstance(sender, e);
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Triggered when the Program is started for the first time.
        /// </summary>
        /// <param name="sender">The sender of the object.</param>
        /// <param name="e">Event arguments.</param>
        private static void OnGUIInitInstance(object sender, InitInstanceEventArgs e)
        {
            GuiProgram program = (GuiProgram)sender;

            eraserClient = new RemoteExecutorServer();
            Application.SafeTopLevelCaptionFormat = S._("Eraser");

            //Load the task list
            try
            {
                if (File.Exists(TaskListPath))
                {
                    using (FileStream stream = new FileStream(TaskListPath, FileMode.Open,
                                                              FileAccess.Read, FileShare.Read))
                    {
                        eraserClient.Tasks.LoadFromStream(stream);
                    }
                }
            }
            catch (InvalidDataException ex)
            {
                File.Delete(TaskListPath);
                MessageBox.Show(S._("Could not load task list. All task entries have " +
                                    "been lost. The error returned was: {0}", ex.Message), S._("Eraser"),
                                MessageBoxButtons.OK, MessageBoxIcon.Error,
                                MessageBoxDefaultButton.Button1,
                                Localisation.IsRightToLeft(null) ?
                                MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign : 0);
            }

            //Decide whether to display any UI.
            GuiArguments arguments = new GuiArguments();

            Args.Parse(program.CommandLine, CommandLinePrefixes, CommandLineSeparators, arguments);
            e.ShowMainForm = !arguments.AtRestart && !arguments.Quiet;

            //Queue tasks meant for running at restart if we are given that command line.
            if (arguments.AtRestart)
            {
                eraserClient.QueueRestartTasks();
            }

            //Run the eraser client.
            eraserClient.Run();

            //Create the main form.
            program.MainForm = new MainForm();
        }
Esempio n. 3
0
            /// <summary>
            /// Runs the event loop of the GUI program, returning true if the program
            /// was started as there were no other instances of the program, or false
            /// if other instances were found.
            /// </summary>
            /// <remarks>
            /// This function must always be called in your program, regardless
            /// of the value of <see cref="IsAlreadyRunning"/>. If this function is not
            /// called, the first instance will never be notified that another was started.
            /// </remarks>
            public void Run()
            {
                //If no other instances are running, set up our pipe server so clients
                //can connect and give us subsequent command lines.
                if (IsFirstInstance)
                {
                    //Initialise and run the program.
                    InitInstanceEventArgs eventArgs = new InitInstanceEventArgs();
                    OnInitInstance(this, eventArgs);
                    if (MainForm == null)
                    {
                        return;
                    }

                    try
                    {
                        //Create the pipe server which will handle connections to us
                        PipeServer = new Thread(ServerMain);
                        PipeServer.Start();

                        //Handle the exit instance event. This will occur when the main form
                        //has been closed.
                        Application.ApplicationExit += OnExitInstance;
                        MainForm.FormClosed         += OnExitInstance;

                        if (eventArgs.ShowMainForm)
                        {
                            Application.Run(MainForm);
                        }
                        else
                        {
                            Application.Run();
                        }
                    }
                    finally
                    {
                        if (PipeServer != null)
                        {
                            PipeServer.Abort();
                        }
                    }
                }

                //Another instance of the program is running. Connect to it and transfer
                //the command line arguments
                else
                {
                    try
                    {
                        NamedPipeClientStream client = new NamedPipeClientStream(".", InstanceID,
                                                                                 PipeDirection.Out);
                        client.Connect(500);

                        StringBuilder commandLineStr = new StringBuilder(CommandLine.Length * 64);
                        foreach (string param in CommandLine)
                        {
                            commandLineStr.Append(string.Format(
                                                      CultureInfo.InvariantCulture, "{0}\0", param));
                        }

                        byte[] buffer = new byte[commandLineStr.Length];
                        int    count  = Encoding.UTF8.GetBytes(commandLineStr.ToString(), 0,
                                                               commandLineStr.Length, buffer, 0);
                        client.Write(buffer, 0, count);
                    }
                    catch (UnauthorizedAccessException)
                    {
                        //We can't connect to the pipe because the other instance of Eraser
                        //is running with higher privileges than this instance. Tell the
                        //user this is the case and show him how to resolve the issue.
                        MessageBox.Show(S._("Another instance of Eraser is already running but it " +
                                            "is running with higher privileges than this instance of Eraser.\n\n" +
                                            "Eraser will now exit."), S._("Eraser"), MessageBoxButtons.OK,
                                        MessageBoxIcon.Information, MessageBoxDefaultButton.Button1,
                                        Localisation.IsRightToLeft(null) ?
                                        MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign : 0);
                    }
                    catch (IOException ex)
                    {
                        MessageBox.Show(S._("Another instance of Eraser is already running but " +
                                            "cannot be connected to.\n\nThe error returned was: {0}", ex.Message),
                                        S._("Eraser"), MessageBoxButtons.OK, MessageBoxIcon.Error,
                                        MessageBoxDefaultButton.Button1,
                                        Localisation.IsRightToLeft(null) ?
                                        MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign : 0);
                    }
                    catch (TimeoutException)
                    {
                        //Can't do much: half a second is a reasonably long time to wait.
                    }
                }
            }
Esempio n. 4
0
        private static void OnGUIInitInstance(object sender, InitInstanceEventArgs e)
        {
            GuiProgram program = (GuiProgram)sender;
               eraserClient = new RemoteExecutorServer();

               EraserSettings settings = EraserSettings.Get();
               Thread.CurrentThread.CurrentUICulture = new CultureInfo(settings.Language);
               Application.SafeTopLevelCaptionFormat = S._("Eraser");

               SettingsCompatibility.Execute();
               try
               {
            if (File.Exists(TaskListPath))
            {
             using (FileStream stream = new FileStream(TaskListPath, FileMode.Open,
              FileAccess.Read, FileShare.Read))
             {
              eraserClient.Tasks.LoadFromStream(stream);
             }
            }
               }
               catch (SerializationException ex)
               {
            System.IO.File.Delete(TaskListPath);
            MessageBox.Show(S._("Could not load task list. All task entries have " +
             "been lost. The error returned was: {0}", ex.Message), S._("Eraser"),
             MessageBoxButtons.OK, MessageBoxIcon.Error,
             MessageBoxDefaultButton.Button1,
             S.IsRightToLeft(null) ? MessageBoxOptions.RtlReading : 0);
               }

               program.MainForm = new MainForm();

               GuiArguments arguments = new GuiArguments();
               Args.Parse(program.CommandLine, CommandLinePrefixes, CommandLineSeparators, arguments);
               e.ShowMainForm = !arguments.AtRestart && !arguments.Quiet;

               if (arguments.AtRestart)
            eraserClient.QueueRestartTasks();

               eraserClient.Run();
        }
 public void Run()
 {
     if (IsFirstInstance)
     {
      try
      {
       PipeServer = new Thread(ServerMain);
       PipeServer.Start();
       InitInstanceEventArgs eventArgs = new InitInstanceEventArgs();
       OnInitInstance(this, eventArgs);
       if (MainForm == null)
        return;
       Application.ApplicationExit += OnExitInstance;
       MainForm.FormClosed += OnExitInstance;
       if (eventArgs.ShowMainForm)
        Application.Run(MainForm);
       else
        Application.Run();
      }
      finally
      {
       PipeServer.Abort();
      }
     }
     else
     {
      try
      {
       NamedPipeClientStream client = new NamedPipeClientStream(".", InstanceID,
        PipeDirection.Out);
       client.Connect(500);
       StringBuilder commandLineStr = new StringBuilder(CommandLine.Length * 64);
       foreach (string param in CommandLine)
        commandLineStr.Append(string.Format(
     CultureInfo.InvariantCulture, "{0}\0", param));
       byte[] buffer = new byte[commandLineStr.Length];
       int count = Encoding.UTF8.GetBytes(commandLineStr.ToString(), 0,
        commandLineStr.Length, buffer, 0);
       client.Write(buffer, 0, count);
      }
      catch (UnauthorizedAccessException)
      {
       MessageBox.Show(S._("Another instance of Eraser is already running but it " +
        "is running with higher privileges than this instance of Eraser.\n\n" +
        "Eraser will now exit."), S._("Eraser"), MessageBoxButtons.OK,
        MessageBoxIcon.Information, MessageBoxDefaultButton.Button1,
        Localisation.IsRightToLeft(null) ?
     MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign : 0);
      }
      catch (IOException ex)
      {
       MessageBox.Show(S._("Another instance of Eraser is already running but " +
        "cannot be connected to.\n\nThe error returned was: {0}", ex.Message),
        S._("Eraser"), MessageBoxButtons.OK, MessageBoxIcon.Error,
        MessageBoxDefaultButton.Button1,
        Localisation.IsRightToLeft(null) ?
     MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign : 0);
      }
      catch (TimeoutException)
      {
      }
     }
 }
 private void OnInitInstance(object sender, InitInstanceEventArgs e)
 {
     if (InitInstance != null)
      InitInstance(sender, e);
 }