【发布时间】:2016-02-08 14:40:32
【问题描述】:
我正在尝试执行一个独立运行的批处理文件。我现在试图通过将其部署为侦听文件夹并使用文件观察程序事件调用批处理文件的 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;
}
我可以从我的事件日志中看到它一直到日志记录 - 在批处理过程开始之前。大概在那之后,该过程通过调用 process.Start() 开始,但随后什么也没有发生。事件日志中没有任何内容,服务仍在运行,即没有崩溃。没有错误。我可以从任务管理器中看到,它确实调用了它应该通过批处理文件调用的 exe,但 exe 只是保留在内存中,具有恒定的内存和 0 CPU 使用率,这表明 exe 没有做任何事情。如果我手动运行批处理文件,它工作正常。知道可能出了什么问题吗?
【问题讨论】:
-
手动运行bat文件时....是否有用户提示?
-
可能存在管理权限问题。请检查一下
-
由于您运行服务(可能没有桌面访问权限),我认为您应该设置
process.StartInfo.CreateNoWindow = true而不是false。但也请查看 Viru 的评论。