Process.HasExited returns true even though process is running?

后端 未结 11 2040
忘掉有多难
忘掉有多难 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:10

    First off, are you sure testprogram does not spawn a process of its own and exit without waiting for that process to finish? We're dealing with some kind of race condition here, and testprogram can be significant.

    Second point I'd like to make is about this - "I need to be absolutely sure that this logfile exists". Well, there is no such thing. You can make your check, and then the file is gone. The common way to address this is not to check, but rather to do what you want to do with the file. Go ahead, read it, catch exceptions, retry if the thing seems unstable and you don't want to change anything. The functional check-and-do does not work well if you have more than one actor (thread or whatever) in the system.

    A bunch of random ideas follows.

    Have you tried using FileSystemWatcher and not depending on process completion?

    Does it get any better if you try reading the file (not checking if it exists, but acting instead) in the process.Exited event? [it shouldn't]

    Is the system healthy? Anything suspicious in the Event Log?

    Can some really aggressive antivirus policy be involved?

    (Can't tell much without seeing all the code and looking into testprogram.)

    0 讨论(0)
  • 2020-12-15 04:12

    I would suggest you to try this way:

    process.Start();
    
    while (!process.HasExited)
    {
        // Discard cached information about the process.
        process.Refresh();
    
        // Just a little check!
        Console.WriteLine("Physical Memory Usage: " + process.WorkingSet64.ToString());
    
        Thread.Sleep(500);
    }
    
    foreach (Process current in Process.GetProcessesByName("testprogram"))
    {
        if ((current.Id == process.Id) && !current.HasExited)
            throw new Exception("Oh oh!");
    }
    

    Anyway... in MSDN page of HasExited I'm reading the following hightlighted note:

    When standard output has been redirected to asynchronous event handlers, it is possible that output processing will not have completed when this property returns true. To ensure that asynchronous event handling has been completed, call the WaitForExit() overload that takes no parameter before checking HasExited.

    That could be somehow linked to your problem as you are redirecting everything.

    0 讨论(0)
  • 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");
    
    0 讨论(0)
  • 2020-12-15 04:18

    For a start, is there an issue with using Process.WaitForExit rather than polling it?

    Anyway, it is technically possible for the process to exit from a usable point of view but the process still be around briefly while it does stuff like flush disk cache. Is the log file especially large (or any operation it is performing heavy on disk writes)?

    0 讨论(0)
  • 2020-12-15 04:20

    I realize this is an old post, but in my quest to find out why my app running the Exited event before the app had even opened I found out something that I though might be useful to people experiencing this problem in the future.

    When a process is started, it is assigned a PID. If the User is then prompted with the User Account Control dialog and selects 'Yes', the process is re-started and assigned a new PID.

    I sat with this for a few hours, hopefully this can save someone time.

    0 讨论(0)
提交回复
热议问题