Marek
Marek

Reputation: 3575

Detecting whether .exe was closed

I need to insert part of code after user closes .exe which I run like this(it is foxpro exe):

private void button1_Click(object sender, EventArgs e)
 {
string openexe= @"C:\Users\marek\Documents\Visual Studio 2012\Projects\tours\tours\bin\Debug\netpokl.exe";
Process.Start(openexe);
}

I thought that it could work something like this:

string otevriExe = @"C:\Users\marek\Documents\Visual Studio 2012\Projects\tours\tours\bin\Debug\netpokl.exe";
string b = Process.Start(otevriExe);
                 b.Closed += b_Closed;
void b_Closed(object sender, EventArgs e)
{
    // mycode    
}

Would someone please help me to improve my code so it would work? Thank you everyone for time and answers.

Upvotes: 0

Views: 104

Answers (5)

digEmAll
digEmAll

Reputation: 57210

Use the Exited event (note: you need to set EnableRaisingEvents to true) e.g. :

private void button1_Click(object sender, EventArgs e)
{
    string openexe= @"C:\Users\marek\Documents\Visual Studio 2012\Projects\tours\tours\bin\Debug\netpokl.exe";
    var proc = new Process();
    proc.StartInfo = new ProcessStartInfo(openexe);
    proc.EnableRaisingEvents = true;
    proc.Exited += new EventHandler(proc_Exited);
    proc.Start();
}

private void proc_Exited(object sender, EventArgs e)
{
    // the process has exited...
}

This is the asynchronous way, for a synchronous approach you can use the WaitForExit method :

private void button1_Click(object sender, EventArgs e)
{
    string openexe= @"C:\Users\marek\Documents\Visual Studio 2012\Projects\tours\tours\bin\Debug\netpokl.exe";
    var proc = new Process();
    proc.StartInfo = new ProcessStartInfo(openexe);
    proc.Start();
    proc.WaitForExit();
    // here the process has exited...
}

Upvotes: 1

Riv
Riv

Reputation: 1859

You could try:

string openexe= @"C:\Users\marek\Documents\Visual Studio 2012\Projects\tours\tours\bin\Debug\netpokl.exe";
Process p = new Process();
p.StartInfo.FileName = openexe;
p.Start();
p.WaitForExit();
//do stuff here

EDIT: seeing as you're starting it on a button click, rather use an event handler:

 private void button1_Click(object sender, EventArgs e)
    {
        Process p = new Process();
        string openexe= @"C:\Users\marek\Documents\Visual Studio 2012\Projects\tours\tours\bin\Debug\netpokl.exe";
        p.StartInfo.FileName = openexe;
        p.EnableRaisingEvents = true;
        p.Exited +=new EventHandler(p_Exited);
        p.Start();            
    }

 private void p_Exited(object sender, EventArgs e)
    {
        //Do stuff here
        MessageBox.Show("Exited");
    }

Upvotes: 1

Ehsan
Ehsan

Reputation: 32671

you can do your stuff onExited event. Like this

public static void Main(string[] args)
{
    MyProcess p = new MyProcess();
    p.StartInfo.FileName = "notepad.exe";
    p.EnableRaisingEvents = true;
    p.Exited += new EventHandler(myProcess_HasExited);
    p.Start();       
}
private static void myProcess_HasExited(object sender, System.EventArgs e)
{
    Console.WriteLine("Process has exited.");
}

Upvotes: 1

SynerCoder
SynerCoder

Reputation: 12766

Set EnableRaisingEvents to True and listen to the Exited event.

string otevriExe = @"C:\Users\marek\Documents\Visual Studio 2012\Projects\tours\tours\bin\Debug\netpokl.exe";
Process b = Process.Start(otevriExe);
b.EnableRaisingEvents = true;
b.Exited += (s, e) => 
{

};

Upvotes: 1

Alessandro D'Andria
Alessandro D'Andria

Reputation: 8868

Try

b.Exited += b_Closed;

void b_Closed(object sender, EventArgs e)
{
    // mycode    
}

Upvotes: 1

Related Questions