在.net中完成三个进程后如何执行一个方法



我使用Process.start和传递不同的参数来运行三个相同的进程。我需要一个逻辑,就像只有在完成这些过程后,我才能执行最后两个名为fourthmethod();sendmail();的方法。如何做到这一点。现有的逻辑一直在抛出这两个方法,但我只需要在完成三个过程后,即三个方法firstmethod();secondmethod();thirdmethod();此代码显示触发三种不同的过程

 // three same test.exe process
    for(int i=0;i<3;i++)
    {
    Process.Start("test.exe",i);
    }

在test.exe主方法中

Main(strin[] args)
{
if(args[0]==0)
{
firstmethod();
}
if(args[0]==1)
{
secondmethod();
}
if(args[0]==2)
{
thirdmethod();
}
fourthmethod();
sendmail();
}
using System;
using System.Diagnostics;
class Program
{
  static int count = 0;
  static object obj = new object();
  static void Main(string[] args)
  {
    Process[] Processes = new Process[3];
    for (int i = 0; i < 3; i++)
    {
        Processes[i] = Process.Start("notepad.exe");
        Processes[i].EnableRaisingEvents = true;
        Processes[i].Exited += Program_Exited;
    }
    Console.ReadLine();
  }
  private static void Program_Exited(object sender, System.EventArgs e)
  {
    lock (obj)
    {
        count++;
    }
    if (count == 3)
        Console.WriteLine("Finised");
  }
}

一种方法:)

更新代码:

 // three same test.exe process
    for(int i=0;i<4;i++)   // the two method should execute only after 3 processes
    {
    Process.Start("test.exe",i);
    }

更新代码Test.exe

Main(strin[] args)
{
if(args[0]==0)
{
firstmethod();
}
if(args[0]==1)
{
secondmethod();
}
if(args[0]==2)
{
thirdmethod();
}
if(args[0]==3)    // i shall increment to 3 only if the first three processes are ran 
{ 
fourthmethod();
sendmail();
 } 
}

最新更新