示例#1
0
        /// <summary>
        /// Given a split Run command this method will launch the process according to the details of the command structure.
        /// </summary>
        /// <param name="commands">An array of arguments for the method (the output of SplitRunCommand).</param>
        public static void ProcessRunCommand(string[] commands)
        {
            if (commands == null)
                throw new ArgumentNullException("commands");

            using (Process process = new Process())
            {
                process.StartInfo.FileName = commands[0];
                process.StartInfo.WorkingDirectory = commands[1];
                process.StartInfo.Arguments = commands[2];
                process.StartInfo.WindowStyle = (ProcessWindowStyle)Enum.Parse(typeof(ProcessWindowStyle), commands[3], true);
                process.StartInfo.CreateNoWindow = bool.Parse(commands[4]);
                process.StartInfo.UseShellExecute = bool.Parse(commands[5]);
                //process.PriorityClass               = ProcessPriorityClass.

                bool waitForExit = bool.Parse(commands[6]);
                bool forceFocus = bool.Parse(commands[7]);

                process.Start();

                // Give new process focus ...
                if (forceFocus && !process.StartInfo.CreateNoWindow &&
                    process.StartInfo.WindowStyle != ProcessWindowStyle.Hidden)
                {
                    FocusForcer forcer = new FocusForcer(process.Id);
                    //forcer.Start();
                    forcer.Force();
                }

                if (waitForExit)
                    process.WaitForExit();
            }
        }
示例#2
0
    private void Launch()
    {
      if (_inConfiguration)
      {
        IrssLog.Info("Can't launch target application, in Configuration");
        return;
      }

      try
      {
        // Check for multiple instances
        if (_oneInstanceOnly)
        {
          foreach (Process process in Process.GetProcesses())
          {
            try
            {
              if (Path.GetFileName(process.MainModule.ModuleName).Equals(Path.GetFileName(_programFile),
                                                                         StringComparison.OrdinalIgnoreCase))
              {
                IrssLog.Info("Can't launch target application, program already running");
                if (_repeatsFocus)
                {
                  IrssLog.Info("Attempting to give focus to target application ...");

                  FocusForcer forcer = new FocusForcer(process.Id);
                  forcer.ForceOnce();
                }
                return;
              }
            }
            catch (Win32Exception ex)
            {
              if (ex.ErrorCode != -2147467259) // Ignore "Unable to enumerate the process modules" errors.
                IrssLog.Error(ex);
            }
            catch (Exception ex)
            {
              IrssLog.Error(ex);
            }
          }
        }

        IrssLog.Info("Launching \"{0}\" ...", _programFile);

        string[] launchCommand = new string[]
                                   {
                                     _programFile,
                                     Path.GetDirectoryName(_programFile),
                                     String.Empty,
                                     Enum.GetName(typeof (ProcessWindowStyle), ProcessWindowStyle.Normal),
                                     false.ToString(),
                                     true.ToString(),
                                     false.ToString(),
                                     true.ToString()
                                   };

        Common.ProcessRunCommand(launchCommand);
      }
      catch (Exception ex)
      {
        IrssLog.Error(ex);
        MessageBox.Show(ex.Message, "Tray Launcher", MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
    }