问题描述
我有,有时调用批处理文件的服务。该批处理文件需要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();
该文件不存在和code,当我运行相同的code。在控制台的工作。然而,当它运行的服务里面,它挂断在 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?
凯文code可以让我得到的输出。我的一个批处理文件仍然挂。
Kevin's code allows me to get output. One of my batch files is still hanging.
C:\ EnterpriseDB公司\ Postgres的\ 8.3 \ BIN \ pg_dump.exe-i -h本地主机-p 5432 -U的Postgres -F p -a -D -v -fC:\ backupcasecocher \ backupdateevent2008.sql -t\公开\,\dateevent \DbTest
另一个批处理文件是:
C:\ EnterpriseDB公司\ Postgres的\ 8.3 \ BIN \ vacuumdb.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?
相反的批处理文件的路径,我写的C:\ EnterpriseDB公司\ Postgres的\ 8.3 \ BIN \ pg_dump.exe为 proc.StartInfo.FileName
和添加的所有参数 proc.StartInfo.Arguments
。结果是不变的,但我看到了 pg_dump.exe
过程窗口。同样,这只是发生在服务中。
Instead of the path of the batch file, I wrote the "C:\EnterpriseDB\Postgres\8.3\bin\pg_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.
我已经运行的管理员组中的用户,都无济于事的服务。我恢复了空
该服务的用户名和密码
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
我创建了一个简单的服务写一个跟踪事件日志,并执行包含目录中有一个批处理文件。现在将挂在 proc.Start();
- 我试图改变帐户从本地系统到用户,然后我设置admnistrator用户名和密码,仍然一无所获。
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调用批处理文件后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!