即使进程正在运行,Process.HasExited 也会返回 true? [英] Process.HasExited returns true even though process is running?

查看:37
本文介绍了即使进程正在运行,Process.HasExited 也会返回 true?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直观察到 Process.HasExited 有时会返回 true,即使进程仍在运行.

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

我下面的代码启动了一个名为testprogram.exe"的进程,然后等待它退出.问题是有时我会抛出异常;似乎即使 HasExited 返回 true 进程本身在系统中仍然存在 - 这怎么可能??

My code below starts a process with name "testprogram.exe" and then waits for it to exit. The problem is that sometimes I get thrown the exception; it seems that even though HasExited returns true the process itself is still alive in the system - how can this be??

我的程序在即将终止之前写入日志文件,因此在读取它之前我需要绝对确定该日志文件存在(也就是进程已终止/完成).不断检查它的存在不是一种选择.

My program writes to a log file just before it terminates and thus I need to be absolutely sure that this log file exists (aka the process has terminated/finished) before reading it. Continuously checking for it's existence is not an option.

// Create new process object
process = new Process();

// Setup event handlers
process.EnableRaisingEvents = true;
process.OutputDataReceived += OutputDataReceivedEvent;
process.ErrorDataReceived += ErrorDataReceivedEvent;
process.Exited += ProgramExitedEvent;

// Setup start info
ProcessStartInfo psi = new ProcessStartInfo
                           {
                               FileName = ExePath,
                               // Must be false to redirect IO
                               UseShellExecute = false,
                               RedirectStandardOutput = true,
                               RedirectStandardError = true,
                               Arguments = arguments
                           };

process.StartInfo = psi;

// Start the program
process.Start();

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

Process[] p = Process.GetProcessesByName( "testprogram" );

if ( p.Length != 0 )
    throw new Exception("Oh oh");

更新:我只是尝试使用 process.WaitForExit() 而不是轮询循环等待,结果完全相同.

UPDATE: I just tried waiting with process.WaitForExit() instead of the polling loop and the result is the exact same.

补充:上面的代码只是为了演示一个更清晰"的问题.说清楚;我的问题不是在将 HasExited 设置为 true 之后,我仍然可以通过 Process.GetProcessesByName("testprogram"); 来控制进程.

Addition: The above code was only to demonstrate a 'clearer' problem alike. To make it clear; my problem is NOT that I still can get a hold of the process by Process.GetProcessesByName( "testprogram" ); after it set HasExited to true.

真正的问题是我在外部运行的程序在它终止之前写入了一个文件(优雅地).我使用 HasExited 来检查进程何时完成,因此我知道我可以读取文件(因为进程退出了!),但似乎 HasExited 返回 真的 甚至有时当程序尚未将文件写入磁盘时.下面是说明确切问题的示例代码:

The real problem is that the program I am running externally writes a file -just before- it terminates (gracefully). I use HasExited to check when the process has finished and thus I know I can read the file (because the process exited!), but it seems that HasExited returns true even sometimes when the program has NOT written the file to disk yet. Here's example code that illustrates the exact problem:

// Start the program
process.Start();

while (!process.HasExited)
    Thread.Sleep( 500 );
// Could also be process.WaitForExit(), makes no difference to the result

// Now the process has quit, I can read the file it has exported
if ( !File.Exists( xmlFile ) )
{
    // But this exception is thrown occasionally, why?
    throw new Exception("xml file not found");
}

推荐答案

我意识到这是一个旧帖子,但在我寻求找出为什么我的应用程序甚至在应用程序打开之前运行 Exited 事件时,我发现了一些我虽然可能对将来遇到此问题的人有用.

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.

当一个进程启动时,它会被分配一个 PID.如果用户随后收到用户帐户控制"对话框的提示并选择是",则该进程将重新启动并分配一个新的 PID.

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.

这篇关于即使进程正在运行,Process.HasExited 也会返回 true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆