Sunday, May 31, 2009

C#: Execute Two Dependant Processes (2 Starts After 1 Finishes)

Gunjan asked about this in one of my other threads:

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Threading;

namespace DriverApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Press Enter to start Notepad (i.e. Process 1)");
Console.ReadKey();
Console.WriteLine("Starting Notepad...");
Thread t1 = new Thread(new ThreadStart(StartProcess1));
t1.Start();
while (t1.IsAlive == true)
{
System.Threading.Thread.Sleep(1000);
}
Console.WriteLine("Notepad Stopped. Starting MSPaint (i.e. Process 2)");
StartProcess2();
Console.WriteLine("MSPaint Stopped. Press Enter to Exit");
Console.ReadKey();
}

static void StartProcess1()
{
Process proc = new Process();
proc.StartInfo.FileName = "Notepad.exe";
proc.Start();
Console.WriteLine("Notepad running...");
proc.WaitForExit();
}

static void StartProcess2()
{
Process proc = new Process();
proc.StartInfo.FileName = "MSPaint.exe";
proc.Start();
Console.WriteLine("MSPaint running...");
proc.WaitForExit();
}
}
}