.net中三个进程完成后如何执行一个方法
How to execute a method after completion of three processes in .net
我是 运行 三个相同的进程,通过使用 Process.start 并传递不同的 arguments.I 需要一种逻辑,就像只有在完成这些进程后我才必须执行最后两个方法named fourthmethod();sendmail();
.How to do this.The 现有逻辑一直在抛出这两个方法,但我只需要在三个过程完成后即三个方法 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();
}
One Way to do it :)
Updated Code :
// 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);
}
Updated code 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();
}
}
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");
}
}
我是 运行 三个相同的进程,通过使用 Process.start 并传递不同的 arguments.I 需要一种逻辑,就像只有在完成这些进程后我才必须执行最后两个方法named fourthmethod();sendmail();
.How to do this.The 现有逻辑一直在抛出这两个方法,但我只需要在三个过程完成后即三个方法 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();
}
One Way to do it :)
Updated Code :
// 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);
}
Updated code 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();
}
}
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");
}
}