1

I'm new to c# and I find it pretty different from my PHP origin, so I may be doing this completely wrong. However from my understanding it's pretty accurate. Basically I need to start a program on button click in my form (that part works perfectly) then when the user closes the program i need to run an action, in this case I'm displaying a message box.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;

namespace usb
{
    class Applications
    {
        //condition variables
        public static bool CalcStatus = false;

        public static void Calc()
        {
            Process CALC;
            CALC = Process.Start("calc.exe");
            if (CALC.HasExited)
            {
                MessageBox.Show("CALC has exited");
                CalcStatus = true;
            }
        }
    }
}

this snippet is part of a class, and another class uses this function, when it is called calc will open the box will not pop up with the alert when calc is closed. Does anyone know what I'm doing wrong? Any more pointers are welcome as I'm still learning :)

Halter
  • 48
  • 2
  • 6
  • 30

2 Answers2

8

You use the Exited event to run some code when the process exits:

CALC.EnableRaisingEvents = true;
CALC.Exited += (sender, args) => MessageBox.Show("CALC has exited");
Servy
  • 202,030
  • 26
  • 332
  • 449
0

Approach 1: Show message with wait

//condition variables
public static bool CalcStatus = false;

public static void Calc()
{
    var calc = new Process
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = "calc.exe"
        }
    };
    calc.Start();
    calc.WaitForExit();
    MessageBox.Show("CALC has exited");
    CalcStatus = true;
}

Approach 2: Show Message without wait

//condition variables
public static bool CalcStatus = false;

public static void Calc()
{
    var calc = new Process
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = "calc.exe"
        },
        EnableRaisingEvents = true,
    };
    calc.Exited += calc_Exited;

    calc.Start();
    CalcStatus = true;
}

static void calc_Exited(object sender, EventArgs e)
{
    MessageBox.Show("CALC has exited");
}
Hadi Sharifi
  • 1,497
  • 5
  • 18
  • 28
  • The first solution is going to be blocking the UI thread, which is a major problem here. – Servy Jan 08 '14 at 20:33
  • You should to use second solution and the first solution is for clarify the differences between two approach. – Hadi Sharifi Jan 08 '14 at 20:38
  • 1) If that's the case then *the answer should say that*, as it is, your answer appears to advocate the use of both approaches. 2) If your goal is to clarify the differences then *clarify the differences*. You haven't explained how they're different, you've just shown both solutions. – Servy Jan 08 '14 at 20:40
  • Thanks @Servy, I explained because user is new in c# development – Hadi Sharifi Jan 08 '14 at 20:44
  • But you *didn't explain anything at all*. That's my point. You just provided two solutions and didn't explain why one of them is wrong and won't solve his problem, how they're different, etc. Had you actually *explained something* then the answer could have value. – Servy Jan 08 '14 at 20:46