C# 批处理文件执行永远不会退出
本文关键字:退出 永远 批处理文件 执行 | 更新日期: 2023-09-27 18:33:45
我正在尝试执行一个自行运行的批处理文件。我现在正在尝试通过将其部署为 Windows 服务来自动化此操作,该服务侦听文件夹并使用文件观察器事件调用批处理文件。这是代码 -
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;
}
我可以从我的事件日志中看到它与日志记录一样远 - 在批处理开始之前。大概在那之后,该过程从调用进程开始。开始(),但随后什么也没发生。事件日志中没有任何内容,服务仍在运行,即未崩溃。没有错误。我可以从任务管理器中看到,它确实调用了它应该通过批处理文件调用的 exe,但 exe 只是保留在内存中,内存恒定,CPU 使用率为 0,这表明 exe 没有做任何事情。如果我手动运行批处理文件,它可以正常工作。知道可能出了什么问题吗?
您禁用了UseShellExecute
.这意味着不能使用 shell 来执行文件。 bat
文件不是可执行文件,它们是 shell 脚本。
由于您无论如何都不会重定向标准 I/O,因此只需启用UseShellExecute
就可以了。