Arup
Arup

Reputation: 55

How to run Two process one after another using C#?

I have a application which will open a two microsoft ppsx file one after another. for that i have used process object to run the files. mention bellow

Process process = Process.Start(@"E:\test\test.ppsx");

I need to run the files in such a way that after finishing the first file ,second file should run automatically. can some one suggest me how can achieve that.

Upvotes: 3

Views: 3191

Answers (4)

StaWho
StaWho

Reputation: 2488

Use Process.WaitForExit()

class Program
{
    static void Main(string[] args)
    {
        Task.Run(() =>
        {
            Process.Start(@"c:\temp\presentation1.pptx").WaitForExit();
        }).ContinueWith(o => 
        {
            Process.Start(@"c:\temp\presentation2.pptx").WaitForExit();
        });
        Console.ReadKey();
    }       
}

Upvotes: 1

Soner Gönül
Soner Gönül

Reputation: 98750

You can use Process.WaitForExit method.

Instructs the Process component to wait indefinitely for the associated process to exit.

Also check Process.Exited event.

Occurs when a process exits.

Process process = Process.Start(@"E:\test\test.ppsx");
process.WaitForExit();

Upvotes: 1

Jibran Khan
Jibran Khan

Reputation: 3256

You should get all the ppsx files from the test directory in the E drive in an array and process on the array of ppsx files according to your requirements.

 string[] files = Directory.GetFiles("your path");

loop through the array and pass each file path to the Process constructor and as lexeRoy said you can WaitForExit.

Upvotes: 0

roybalderama
roybalderama

Reputation: 1640

You can use WaitForExit method to wait to end process (Something like this):

var process1 = Process.Start(...);
process1.WaitForExit();

var process2 = Process.Start(...);

or subscribe into a Process.Exited event and execute another process after the first one. Check this for your reference.

Upvotes: 4

Related Questions