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();
}
}
}

1 comment:

  1. Hello KC. Great job !!

    BTW can you tell me how to launch process from project resources?
    For example diff.exe (with libiconv2.dll and libintl3.dll). Maybe extracting them to temp directory?

    ReplyDelete

Please use your common sense before making a comment, and I truly appreciate your constructive criticisms.