Thread.Sleep()导致Process.Kill()出现问题

时间:2013-01-29 13:31:48

标签: c# process

我添加了一行代码来使线程休眠3秒。

如果我取消注释突出显示的两行代码中的任何一行,我会得到一个错误 - 我知道它与添加睡眠有关 - 但为什么呢?

我认为myProcess本身就是一个线程 - 这个线程正在睡觉吗? 我需要更改什么才能使IE保持打开状态3秒然后关闭?

class MyProcess {


    public static void Main() {

        Process myProcess = new Process();
        ProcessStartInfo ps = new ProcessStartInfo();

        try {

            ps.FileName = @"IExplore.exe";
            ps.WindowStyle = ProcessWindowStyle.Normal;
            myProcess.StartInfo = ps;
            myProcess.Start();

        } catch(Exception e) {
            Console.WriteLine(e.Message);
        }

        Thread.Sleep(3000);

        //>>>>>>>>following throw an error
        //myProcess.Kill();
        //myProcess.CloseMainWindow();
        //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

        Console.WriteLine("press [enter] to exit");
        Console.Read();
    }
}

这是我收到的错误

  

无法处理请求,因为该流程已退出

enter image description here

1 个答案:

答案 0 :(得分:2)

如果操作无法完成,您将收到InvalidOperationException。在您的代码中,如果流程已退出,您可能会收到InvalidOperationException,然后才能调用myProcess.Kill();myProcess.CloseMainWindow();

当进程已经退出时,您无法终止该进程。要解决此问题,请避免在线程空闲时关闭进程。您可以在执行命令之前捕获异常或检查进程是否已退出。

示例

class MyProcess
{
    public static void Main()
    {

        Process myProcess = new Process(); //Initialize a new Process of name myProcess
        ProcessStartInfo ps = new ProcessStartInfo(); //Initialize a new ProcessStartInfo of name ps

        try
        {
            ps.FileName = @"IExplore.exe"; //Set the target file name to IExplore.exe
            ps.WindowStyle = ProcessWindowStyle.Normal; //Set the window state when the process is started to Normal
            myProcess.StartInfo = ps; //Associate the properties to pass to myProcess.Start() from ps
            myProcess.Start(); //Start the process

        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message); //Write the exception message
        }

        Thread.Sleep(3000); //Stop responding for 3 seconds

        if (myProcess.HasExited) //Continue if the process has exited
        {
            //The process has exited
            //Console.WriteLine("The process has exited");
        }
        else //Continue if the process has not exited
        {
            //myProcess.CloseMainWindow(); //Close the main Window
            myProcess.Kill(); //Terminate the process
        }

        Console.WriteLine("press [enter] to exit"); //Writes press [enter] to exit
        Console.Read(); //Waits for user input
    }
}

通知:如果您运行的是IE8,您可能会注意到Internet Explorer 8已实施更改,其中多个打开的浏览器窗口或框架在所有打开的选项卡和窗口中共享相同的会话cookie,包括新的IE框架窗口由用户打开。

您可以使用参数 -nomerge 运行 IExplore.exe 进程以运行新的IE8 Window会话,并避免将您创建的新进程与之前创建的进程合并(IE8创建的默认进程与其他进程合并)。因此,拥有一个可以从应用程序而不是由IE8创建的新进程。

示例

Process myProcess = new Process(); //Initialize a new Process of name myProcess
ProcessStartInfo ps = new ProcessStartInfo(); //Initialize a new ProcessStartInfo of name ps

try
{
    ps.FileName = @"IExplore.exe"; //Set the target file name to IExplore.exe
    ps.Arguments = "-nomerge";
    ps.WindowStyle = ProcessWindowStyle.Normal; //Set the window state when the process is started to Normal
    myProcess.StartInfo = ps; //Associate the properties to pass to myProcess.Start() from ps
    myProcess.Start(); //Start the process
}
catch (Exception e)
{
    Console.WriteLine(e.Message); //Write the exception message
}

Thread.Sleep(3000); //Stop responding for 3 seconds

if (myProcess.HasExited) //Continue if the process has exited
{
    //The process has exited
    //Console.WriteLine("The process has exited");
}
else //Continue if the process has not exited; evaluated if the process was not exited.
{
    myProcess.Kill(); //Terminate the process
}

此外,如果IE8的进程意外终止,则进程退出代码将等于-11如果用户终止,0如果退出传递控制权另一个例子。

谢谢, 我希望你觉得这很有帮助:)