Пример #1
0
        /// <summary>
        /// Immediately stops the associated process.
        /// </summary>
        /// <exception cref="System.InvalidOperationException">Cannot Terminate Non-Running Process.</exception>
        /// <exception cref="System.Exception">WMI command did not successfully complete</exception>
        /// <exception cref="System.ObjectDisposedException">Object Has Been Disposed</exception>
        public void Kill()
        {
            if (!this.IsProcessRunning)
            {
                throw new InvalidOperationException("Cannot Terminate Non-Running Process.");
            }

            /* Command: Use task kill to end the process.
             * CMD
             * - /c : cmd carry out string command and terminate.
             * TASKKILL
             * - /f : Process(es) are forcefully terminated. Redundant for remote processes, all remote processes are forcefully terminated.
             * - /pid : The process ID of the process to be terminated.
             * - /t : Tree kill. terminate all child processes along with the parent process.
             */

            var command = string.Format("cmd /c \"taskkill /f /pid {0}\" /t", processId);

            ManagementBaseObject outParams = wmiWrapper.RunCommand(command);
            var returnCode = outParams.GetReturnValue();
            if (returnCode != WmiReturnValue.SuccessfullCompletion)
            {
                throw new Exception(string.Format("Starting Taskkill returned: {0}.", returnCode));
            }

            this.IsProcessRunning = false;
        }
Пример #2
0
        /// <summary>
        /// Starts the Process.
        /// </summary>
        /// <exception cref="System.InvalidOperationException">Cannot Start Running Process.</exception>
        /// <exception cref="System.Exception">WMI command did not successfully complete</exception>
        /// <exception cref="System.ObjectDisposedException">Object Has Been Disposed</exception>
        public void Start()
        {
            if (this.IsProcessRunning)
            {
                throw new InvalidOperationException("Cannot Start Running Process.");
            }

            ManagementBaseObject outParams = this.wmiWrapper.RunCommand(this.startCommand);
            var returnCode = outParams.GetReturnValue();
            if (returnCode != WmiReturnValue.SuccessfullCompletion)
            {
                throw new Exception(string.Format("StartProcess returned: {0}.", returnCode));
            }

            this.processId = outParams.GetPid();
            this.IsProcessRunning = true;
        }