【问题标题】:How to know the completion of the three methods invoked by process.start() in .net(c#) [duplicate]如何知道.net(c#)中process.start()调用的三个方法的完成情况[重复]
【发布时间】:2016-07-13 21:43:15
【问题描述】:

我有两个名为first.exesecond.exe 的exe(两个控制台应用程序),其中first 通过使用Process.start() 并传递不同的参数来调用second exe 三次。这三个执行完成后我想要运行一个方法(只有在完成三个方法之后)。如何做到这一点。 这就是我在second.exe中处理三个调用的方式@

 static void Main(string[] args)
        {


            if (args[0] == "TC" && args[1] == "CS")
            {
                Processfiles("TCS");
            }
            if (args[0] == "RC" && args[1] == "CS")
            {
                Zipfiles("RCS");
            }
            if (args[0] == "CC" && args[1] == "CS")
            {
                Leveragefiles("CTS");
            }

            Downloadfiles();
}

这里我的问题是Processfiles("TCS");,Zipfiles("RCS");,Leveragefiles("CTS");三个方法完成后如何执行Downloadfiles();方法。这里三种方法的完成时间因时间不同而不同

用于启动second.exe的代码是

   ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = System.Configuration.ConfigurationManager.AppSettings["PathExe"];
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dtr in ds.Tables["testG"].Rows)
                {
                    startInfo.Arguments = dtr["TC_CODE"].ToString() + " " + dtr["CC_CODE"].ToString();
                    Process.Start(startInfo);
                    // System.Diagnostics.Process.Start(System.Configuration.ConfigurationManager.AppSettings["PathExe"], dtr["VENDOR_CODE"].ToString() + " " + dtr["TECH_CODE"].ToString());
                }
            }
        }
        catch (Exception ex)
        {
            mail.SendMail2(System.Configuration.ConfigurationManager.AppSettings["EmailFrom"], System.Configuration.ConfigurationManager.AppSettings["EmailCc"], null, ex.Message, "error", true);
            log.Error(ex.Message);
        }

【问题讨论】:

  • 你能显示你用来启动second.exe的代码吗?
  • @BJ Myers,我已经编辑了我的问题

标签: c# .net process console-application


【解决方案1】:

您可以为此使用线程任务。 按原样在任务上使用条件运算符

using System.Threading.Tasks;
...

Task.Factory.StartNew(() => Processfiles("TCS"));
Task.Factory.StartNew(() => Zipfiles("RCS"));
Task.Factory.StartNew(() => Leveragefiles("CTS"));
Task.WaitAll();//to make sure all the task is complete
 Downloadfiles();

【讨论】:

  • 但是调用来自process.start(),你的意思是在if循环里面?
  • 是的,只要在使用时使用诊断即可确定。
猜你喜欢
  • 2016-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-15
  • 2013-09-11
  • 1970-01-01
  • 2012-04-28
相关资源
最近更新 更多