Process.HasExited returns true even though process is running?

后端 未结 11 2039
忘掉有多难
忘掉有多难 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 03:57

    I know, this is an old post but maybe I can help someone.
    The Process class may behave unexpectedly! HasExited will return true if the process has exited or if the process runs with administrator privileges and your program only has user privileges.

    0 讨论(0)
  • 2020-12-15 03:58

    If you have web application, and your external program/process is generating files (write to disk) check if your IIS have rights to write to that folder if not on properties security add permission for your IIS user, that was the reason in my case, i was receiving process.HasExited =true, but produced files from the process was not completed, after struggling for a while i add full permissions to the folder where process was writhing and process.Refresh() as Zarathos described from above and everything was working as expected.

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

    As per MSDN documentation for HasExited.

    If a handle is open to the process, the operating system releases the process memory when the process has exited, but retains administrative information about the process, such as the handle, exit code, and exit time.

    Probably not related, but it's worth noting.

    If it's only a problem 1/10 of the time, and the process disappears after a second anyway, depending on your usage of HasExited, try just adding another delay after the HasExited check works, like

    while (!process.HasExited)
        DoStuff();
    Thread.Sleep(500);
    Cleanup();
    

    and see if the problem persists.

    Personally, I've always just used the Exited event handler instead of any kind of polling, and a simplistic custom wrapper around System.Diagnostics.Process to handle things like thread safety, wrapping a call to CloseMainWindow() followed by WaitForExit(timeout) and finally Kill(), logging, et cetera, and never encountered a problem.

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

    Use process_name.Refresh() before checking whether process has exited or not. Refresh() will clear all the cached information related to the process.

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

    So just for a further investigation into the root cause of the problem you should maybe check out what's really happening by using Process Monitor. Simply start it and include the external program and your own tool and let it record what happens.

    Within the log you should see how the external tool writes to the output file and how you open that file. But within this log you should see in which order all these accesses happen.

    The first thing that came to my mind is that the Process class doesn't lie and the process is really gone when it tells so. So problem is that at this point in time it seems that the file is still not fully available. I think this is a problem of the OS, cause it holds some parts of the file still within a cache that is not fully written onto the disk and the tool has simply exited itself without flushing its file handles.

    With this in mind you should see within the log that the external tool created the file, exited and AFTER that the file will be flushed/closed (by the OS [maybe remove any filters when you found this point within the log]).

    So if my assumptions are correct the root cause would be the bad behavior of your external tool which you can't change thus leading to simply wait a little bit after the process has exited and hope that the timeout is long enough to get the file flushed/closed by the OS (maybe try to open the file in a loop with a timeout till it succeeded).

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

    Maybe the problem is in the testprogram? Does this code nicely flush/close etc.? It seems to me if testprogram writes a file to disk, the file should at least be available (empty or not)

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