Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 运行嵌套的批处理文件_C#_.net_Batch File - Fatal编程技术网

C# 运行嵌套的批处理文件

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");

我想运行两个批处理文件,它们启动一个mongodb和一个nodejs服务器

为了运行节点服务器,我需要等待mongodb被执行

到目前为止,我可以像这样运行mongodb:

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

希望它能帮助您……

我不熟悉所涉及的工具,因此无法提供复制可复制的代码,但我会尝试实现此流程(非常伪代码):

  • 创建启动MongoDB的进程
  • 等一会儿(我想是5秒钟)
  • 尝试连接到MongoDB
  • 如果连接成功,转到7
  • 等一会儿
  • 转到3
  • 创建一个进程来启动node.js
  • 等一会儿
  • 尝试从node.js获取某些内容
  • 如果获取成功,转到13
  • 等一会儿
  • 转到9
  • 出口
  • 如果您想提供一些反馈,那么“等待”步骤可能会发生在不同的线程上(例如,我认为一个
    BackgroundWorker
    具有定时
    ProgressChanged
    调用)


    仔细想想,这可能适用于启动但无法立即使用的任何工具组合…

    WaitForExit()方法之后创建一个新的
    Process
    对象如何?顺便说一句,您的
    string.Format
    在您的案例中是无用的。这不起作用。Mongodb需要一些时间来初始化。节点服务器启动太早。mongo进程没有退出,它保持打开状态。是否有可能检查进程是否正在等待输入或空闲运行。。诸如此类?正如Alex所说,尝试连接到MongoDB以查看它是否已启动并运行。不幸的是,第二个进程启动太早,此时MongoDB尚未完成初始化。哦,我没有看到Alex发布…;)