调用批处理文件后服务在 WaitForExit 挂断 [英] Service hangs up at WaitForExit after calling batch file

查看:21
本文介绍了调用批处理文件后服务在 WaitForExit 挂断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项服务,有时会调用批处理文件.批处理文件需要 5-10 秒才能执行:

I have a service that sometimes calls a batch file. The batch file takes 5-10 seconds to execute:

System.Diagnostics.Process proc = new System.Diagnostics.Process(); // Declare New Process
    proc.StartInfo.FileName = fileName;
    proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    proc.StartInfo.CreateNoWindow = true;
    proc.Start();
    proc.WaitForExit();

当我在控制台中运行相同的代码时,该文件确实存在并且代码有效.然而,当它在服务内部运行时,它在 WaitForExit() 处挂断.我必须从进程中终止批处理文件才能继续.(我确定该文件存在,因为我可以在进程列表中看到它.)

The file does exist and the code works when I run the same code in-console. However when it runs inside the service, it hangs up at WaitForExit(). I have to kill the batch file from the Process in order to continue. (I am certain the file exists, as I can see it in the processes list.)

我该如何解决这个问题?

How can I fix this hang-up?

Kevin 的代码允许我获得输出.我的一个批处理文件仍然挂起.

Kevin's code allows me to get output. One of my batch files is still hanging.

"C:EnterpriseDBPostgres8.3inpg_dump.exe" -i -h localhost -p 5432 -U postgres -F p -a -D -v -f "c:ackupcasecocherackupdateevent2008.sql" -t ""public"."dateevent"" "DbTest"

"C:EnterpriseDBPostgres8.3inpg_dump.exe" -i -h localhost -p 5432 -U postgres -F p -a -D -v -f "c:ackupcasecocherackupdateevent2008.sql" -t ""public"."dateevent"" "DbTest"

另一个批处理文件是:

"C:EnterpriseDBPostgres8.3invacuumdb.exe" -U postgres -d DbTest

"C:EnterpriseDBPostgres8.3invacuumdb.exe" -U postgres -d DbTest

我检查了路径,postgresql 路径没问题.输出目录确实存在并且仍然在服务之外工作.有什么想法吗?

I have checked the path and the postgresql path is fine. The output directory does exist and still works outside the service. Any ideas?

代替批处理文件的路径,我为proc.StartInfo.FileName写了C:EnterpriseDBPostgres8.3inpg_dump.exe"并将所有参数添加到proc.StartInfo.Arguments.结果没有改变,但我在进程窗口中看到了pg_dump.exe.同样,这只发生在服务内部.

Instead of the path of the batch file, I wrote the "C:EnterpriseDBPostgres8.3inpg_dump.exe" for the proc.StartInfo.FileName and added all parameters to proc.StartInfo.Arguments. The results are unchanged, but I see the pg_dump.exe in the process window. Again this only happens inside the service.

我已经使用管理员组中的用户运行该服务,但无济于事.我为服务的用户名和密码恢复了 null

I have run the service with a user in the administrator group, to no avail. I restored null for the service's username and password

我创建了一个简单的服务来在事件日志中写入跟踪并执行一个包含dir"的批处理文件.它现在将挂在 proc.Start(); - 我尝试将帐户从 LocalSystem 更改为 User 并且我设置了管理员用户和密码,但仍然没有.

I created a simple service to write a trace in the event log and execute a batch file that contains "dir" in it. It will now hang at proc.Start(); - I tried changing the Account from LocalSystem to User and I set the admnistrator user and password, still nothing.

推荐答案

这是我用来执行批处理文件的:

Here is what i use to execute batch files:

proc.StartInfo.FileName                 = target;
proc.StartInfo.RedirectStandardError    = true;
proc.StartInfo.RedirectStandardOutput   = true;
proc.StartInfo.UseShellExecute          = false;

proc.Start();

proc.WaitForExit
    (
        (timeout <= 0)
            ? int.MaxValue : timeout * NO_MILLISECONDS_IN_A_SECOND *
                NO_SECONDS_IN_A_MINUTE
    );

errorMessage = proc.StandardError.ReadToEnd();
proc.WaitForExit();

outputMessage = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();

我不知道这是否对您有用,但我没有挂起的问题.

I don't know if that will do the trick for you, but I don't have the problem of it hanging.

这篇关于调用批处理文件后服务在 WaitForExit 挂断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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