C# 运行嵌套的批处理文件
我想运行两个批处理文件,它们启动一个mongodb和一个nodejs服务器 为了运行节点服务器,我需要等待mongodb被执行 到目前为止,我可以像这样运行mongodb:C# 运行嵌套的批处理文件,c#,.net,batch-file,C#,.net,Batch File,我想运行两个批处理文件,它们启动一个mongodb和一个nodejs服务器 为了运行节点服务器,我需要等待mongodb被执行 到目前为止,我可以像这样运行mongodb: static void Main(string[] args) { Process proc = null; try { string targetDir = string.Format(@"D:\nodejs\host");
static void Main(string[] args)
{
Process proc = null;
try
{
string targetDir = string.Format(@"D:\nodejs\host");
proc = new Process();
proc.StartInfo.WorkingDirectory = targetDir;
proc.StartInfo.FileName = "mongo.bat";
proc.StartInfo.CreateNoWindow = false;
proc.Start();
proc.WaitForExit();
}
catch (Exception ex)
{
Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
}
}
mongo完成初始化后,如何执行下一个批处理文件?您可以像这样使用WaitForExit()
proc.Start();
proc.WaitForExit(1000 * 60 * 5); // wait for 5 minutes.
或
proc = new Process();
while (!proc.HasExited)
{
//mongoDB started
}
// Now start node server
希望它能帮助您……我不熟悉所涉及的工具,因此无法提供复制可复制的代码,但我会尝试实现此流程(非常伪代码):
BackgroundWorker
具有定时ProgressChanged
调用)
仔细想想,这可能适用于启动但无法立即使用的任何工具组合…在
WaitForExit()方法之后创建一个新的Process
对象如何?顺便说一句,您的string.Format
在您的案例中是无用的。这不起作用。Mongodb需要一些时间来初始化。节点服务器启动太早。mongo进程没有退出,它保持打开状态。是否有可能检查进程是否正在等待输入或空闲运行。。诸如此类?正如Alex所说,尝试连接到MongoDB以查看它是否已启动并运行。不幸的是,第二个进程启动太早,此时MongoDB尚未完成初始化。哦,我没有看到Alex发布…;)