C#批处理文件执行永远不会退出
我正在尝试执行一个批处理文件,它自己运行。我现在正试图通过将其部署为一个windows服务来实现自动化,该服务侦听文件夹并使用file watcher事件调用批处理文件。这是代码-C#批处理文件执行永远不会退出,c#,process,C#,Process,我正在尝试执行一个批处理文件,它自己运行。我现在正试图通过将其部署为一个windows服务来实现自动化,该服务侦听文件夹并使用file watcher事件调用批处理文件。这是代码- void fileSystemWatcher_Created(object sender, FileSystemEventArgs e) { ServiceEventLog.WriteEntry(TheServiceName + " Inside fileSystemWatcher_Created() - "
void fileSystemWatcher_Created(object sender, FileSystemEventArgs e)
{
ServiceEventLog.WriteEntry(TheServiceName + " Inside fileSystemWatcher_Created() - ");
if (e.Name.Trim().ToUpper().Contains("FU4DGF_TRADES"))
{
try
{
Utilities.SendEmail("IAMLDNSMTP", 25, "desmond.quilty@investecmail.com", "IAMITDevelopmentServices@investecmail.com", "Ben.Howard@investecmail.com", "prasad.matkar@investecmail.com", "StatPro BatchFile Execution Started ", "");
int exitCode;
// ProcessStartInfo processInfo;
ServiceEventLog.WriteEntry(TheServiceName + " Before creation of instance of Batch process - ");
Process process = new Process();
process.StartInfo.FileName = @"C:\Program Files (x86)\StatPro Suite\MonthlyUpload.bat";
process.StartInfo.RedirectStandardOutput = false;
process.StartInfo.RedirectStandardError = false;
process.StartInfo.CreateNoWindow = false;
process.StartInfo.WorkingDirectory = @"C:\Program Files (x86)\StatPro Suite";
process.StartInfo.UseShellExecute = false;
ServiceEventLog.WriteEntry(TheServiceName + " Before start of Batch process - ");
process.Start();
ServiceEventLog.WriteEntry(TheServiceName + " After start of Batch process - ");
process.WaitForExit();
//while (!process.HasExited)
//{
// System.Threading.Thread.Sleep(100);
//}
ServiceEventLog.WriteEntry(TheServiceName + " After process.close - ");
System.Environment.ExitCode = process.ExitCode;
}
我可以从我的事件日志中看到,它可以追溯到日志记录——在批处理开始之前。大概在这之后,流程通过调用process.Start()启动,但随后什么也没有发生。事件日志中没有任何内容,服务仍在运行,即未崩溃。没有错误。我可以从任务管理器中看到,它确实调用了它应该通过批处理文件调用的exe,但exe只是保留在内存中,内存不变,CPU使用率为0,这表明exe没有做任何事情。如果我手动运行批处理文件,它可以正常工作。知道会出什么问题吗?您禁用了
UseShellExecute
。这意味着您不能使用shell来执行文件<代码>bat文件不是可执行文件,它们是shell脚本
由于您无论如何都不会重定向标准I/O,只需启用
UseShellExecute
,就可以了。当您手动运行bat文件时……是否会显示任何用户提示?可能是重复的,可能存在管理权限问题。请检查一下,因为您运行的服务(可能没有桌面访问权限),我认为您应该设置process.StartInfo.CreateNoWindow=true
,而不是false
。但也要检查Viru的评论。没有用户提示。我确定没有。我还将尝试禁用UseShellExecute,看看这是否有帮助。将更新您。@PrasadMatkar您已经禁用了它-您需要启用它:)