调用批处理文件后,服​​务在WaitForExit挂断

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


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()。我必须从“进程”中删除该批处理文件才能继续。(我确定该文件存在,因为我可以在进程列表中看到它。)


我该如何解决此挂断?


更新#1:

凯文的代码允许我获取输出。我的批处理文件之一仍然挂起。


“ C:\ EnterpriseDB \ Postgres \ 8.3 \ bin \ pg_dump.exe” -i -h本地主机-p 5432 -U postgres -F p -a -D -v -f“ c:\ backupcasecocher \ backupdateevent2008.sql” -t “ \” public \“。\” dateevent \“”“ DbTest”


另一个批处理文件是:


“ C:\ EnterpriseDB \ Postgres \ 8.3 \ bin \ vacuumdb.exe” -U postgres -d DbTest


我已经检查了路径,并且postgresql路径很好。输出目录确实存在,并且仍然可以在服务外部使用。有任何想法吗?


更新#2:

代替批处理文件的路径,我为编写了“ C:\ EnterpriseDB \ Postgres \ 8.3 \ bin \ pg_dump.exe” proc.StartInfo.FileName,并将所有参数添加到proc.StartInfo.Arguments。结果没有变化,但是pg_dump.exe在进程窗口中看到。同样,这仅在服务内部发生。


更新#3:

我已经与管理员组中的用户一起运行了该服务,但无济于事。我已还原null该服务的用户名和密码


更新#4:

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


扬帆大鱼
浏览 375回答 3
3回答

千巷猫影

这是我用来执行批处理文件的内容:proc.StartInfo.FileName&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= target;proc.StartInfo.RedirectStandardError&nbsp; &nbsp; = true;proc.StartInfo.RedirectStandardOutput&nbsp; &nbsp;= true;proc.StartInfo.UseShellExecute&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = false;proc.Start();proc.WaitForExit&nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; (timeout <= 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? int.MaxValue : timeout * NO_MILLISECONDS_IN_A_SECOND *&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NO_SECONDS_IN_A_MINUTE&nbsp; &nbsp; );errorMessage = proc.StandardError.ReadToEnd();proc.WaitForExit();outputMessage = proc.StandardOutput.ReadToEnd();proc.WaitForExit();我不知道这是否可以帮到您,但是我没有问题的困扰。

德玛西亚99

using System;&nbsp; &nbsp; using System.Collections.Generic;&nbsp; &nbsp; using System.Linq;&nbsp; &nbsp; using System.Text;&nbsp; &nbsp; using System.Diagnostics;&nbsp; &nbsp; namespace VG&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; class VGe&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [STAThread]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Process proc = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string targetDir = string.Format(@"D:\adapters\setup");//this is where mybatch.bat lies&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; proc = new Process();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; proc.StartInfo.WorkingDirectory = targetDir;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; proc.StartInfo.FileName = "mybatch.bat";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; proc.StartInfo.Arguments = string.Format("10");//this is argument&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; proc.StartInfo.CreateNoWindow = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; proc.Start();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; proc.WaitForExit();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch (Exception ex)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Exception Occurred :{0},{1}", ex.Message,ex.StackTrace.ToString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

慕桂英3389331

批处理文件做什么?您确定进程具有足够的特权来启动以执行批处理文件吗?服务可能会受到限制。另外,请确保在复制命令中是否正在执行类似我们的操作,以覆盖执行以下操作的文件:echo Y | copy foo.log c:\backup\另外,请确保使用批处理命令等的完整路径。如果批处理文件正在以某种“控制台”模式启动GUI应用程序,那么这也可能是一个问题。请记住,服务没有“桌面”(除非您启用了“与桌面交互”)才能绘制任何类型的窗口或消息框。在程序中,您可能需要打开stdout和stderr管道,并在执行过程中从中读取它们,以防收到任何错误消息或任何其他内容。WebServices可能会以IUSR帐户或匿名帐户的身份执行,因此这可能对您来说是个问题。如果您在控制台中运行它时可以运行,那只是第一步。:)我不记得是System.Diagnostics。是否仅在调试中可用。可能不是,但是其中一些可能是。我必须检查一下。希望这能给您一些想法。拉里
打开App,查看更多内容
随时随地看视频慕课网APP