Process.HasExited returns true even though process is running?

后端 未结 11 2091
忘掉有多难
忘掉有多难 2020-12-15 03:41

I have been observing that Process.HasExited sometimes returns true even though the process is still running.

My code below starts a proces

11条回答
  •  既然无缘
    2020-12-15 04:13

    There's two possibilities, the process object continues to hold a reference to the process, so it has exited, but it hasn't yet been deleted. Or you have a second instance of the process running. You should also compare the process Id to make sure. Try this.

        ....
    
        // Start the program
        process.Start();
    
    
        while (!process.HasExited)
            Thread.Sleep( 500 );
    
        Process[] p = Process.GetProcessesByName( "testprogram" );
        if ( p.Length != 0 && p[0].Id == process.id && ! p[0].HasExited)
            throw new Exception("Oh oh");
    

提交回复
热议问题