Process.HasExited returns true even though process is running?

后端 未结 11 2108
忘掉有多难
忘掉有多难 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.)

提交回复
热议问题