我在共享位置有一个批处理文件,我正在从.net代码执行但是批处理文件没有执行正在发生的事情是批处理文件的整个代码不起作用只是部分代码工作。我假设.net代码执行正在完成并停止批处理文件的执行。 下面是我的.net代码
private static void ExecuteCommand(string command, string arguments)
{
try
{
Console.WriteLine("Code execution starts");
Console.WriteLine("command: " + command);
Console.WriteLine("arguments: " + arguments);
var process = new Process();
process.StartInfo.FileName = command;
if (!string.IsNullOrEmpty(arguments))
{
process.StartInfo.Arguments = arguments;
}
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
//process.StartInfo.CreateNoWindow = false;
//process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
process.StartInfo.UseShellExecute = true;
process.EnableRaisingEvents = true;
//process.StartInfo.RedirectStandardError = true;
//process.StartInfo.RedirectStandardOutput = true;
string stdError;
// process.Start();
Thread.Sleep(60000);
ThreadPool.QueueUserWorkItem(delegate
{
process.Start();
// process.WaitForExit();
});
//Thread.Sleep(60000);
//ThreadStart ths = new ThreadStart(delegate()
//{
// process.Start();
// //process.BeginOutputReadLine();
// //stdError = process.StandardError.ReadToEnd();
//});
//process.Start();
//process.BeginOutputReadLine();
//stdError = process.StandardError.ReadToEnd();
Console.WriteLine("Code execution ends");
}
catch (Exception e)
{
Console.WriteLine("Code execution error: "+e.Message +"||"+e.InnerException.ToString());
}
}
要求是开始执行批处理文件,并等待批处理文件执行的任何输出或完成。当我从命令提示符或Windows资源管理器运行批处理文件时它工作正常。无法在.net代码中使用cmd.exe,因为我们需要在批处理文件不存在或权限问题时处理异常
答案 0 :(得分:0)
我认为您可以使用WaitForExit
方法,而不是使用可能或不可能的睡眠。
有关它的更多信息:http://msdn.microsoft.com/en-us/library/ty0d8k56.aspx
如果这不起作用,您可以使用一种解决方法,例如使用在流程开始之前创建的文件,并在批处理结束时删除批处理文件,并由FileSystemWatcher
定期检查({{ 3}})作为检查过程是否完成的方法。