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

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

问题描述

我已经观察到 Process.HasExited 有时会返回真正即使进程仍在运行。



我的代码下面将启动一个进程名称为testprogram.exe,然后等待其退出。问题是,有时我得到抛出的异常;看来,即使 HasExited 收益真正的过程本身还是在系统中活着 - 这怎么可能?



我的程序只是将其终止,因此我需要绝对确保该日志文件存在(又名进程已终止/已完成)读数之前之前写入日志文件它。不断地检查它的存在是不是一种选择。

  //创建新的进程对象
流程=新工艺() ;

//设置事件处理
process.EnableRaisingEvents = TRUE;
process.OutputDataReceived + = OutputDataReceivedEvent;
process.ErrorDataReceived + = ErrorDataReceivedEvent;
process.Exited + = ProgramExitedEvent;

//设置启动信息
的ProcessStartInfo PSI =新的ProcessStartInfo
{
=文件名ExePath,
//一定是假的重定向IO
UseShellExecute =假,
RedirectStandardOutput = TRUE,
RedirectStandardError = TRUE,
参数=参数
};

process.StartInfo =磅;

//启动程序
的Process.Start();

,而(process.HasExited!)
的Thread.Sleep(500);

过程[] P = Process.GetProcessesByName(testprogram);

如果(!p.Length = 0)
抛出新的异常(哦,哦);



更​​新:我只是试图用 process.WaitForExit等待()而不是轮询循环,结果是完全一样的。



增加:上面的代码,只是为了证明一样一个更清晰的问题。要清楚;我的问题是不是我仍然可以通过获取进程的举行Process.GetProcessesByName(testprogram); 将其设置在 HasExited 为true。



真正的问题是,我运行外部程序写 - 仅仅直至─它终止(正常)的文件。我使用 HasExited 来检查时,这个过程已经完成,因此我知道我可以读取该文件(因为进程退出!),但似乎 HasExited 收益真正有时甚至当程序不写入到磁盘文件呢。下面是说明确切的问题示例代码:

  //启动程序
的Process.Start();

,而(process.HasExited!)
的Thread.Sleep(500);
//也可能是process.WaitForExit(),使得该结果

//现在这个过程已经退出没有什么区别,我能读懂它已经出口
如果文件(!File.Exists(XMLFILE))
{
//但是这个例外是偶尔抛出,为什么呢?
抛出新的异常(未找到XML文件);
}


解决方案

我意识到这是一个老职位,但在我的追求,以找出为什么我的应用程序运行已退出事件之前的应用程序甚至已经开了,我发现了一些东西,我虽然可能是人们经历着未来这个问题非常有用。



当一个处理开始时,它被分配一个PID。的如果用户然后用'是',这个过程是重新开始,并分配一个新的PID用户帐户控制对话框,选择提示。



我坐在这了几个小时,希望这样可以节省时间的人。


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

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");

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

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.

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");
}

解决方案

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.

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

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