Hello all,

This is my first post and I am counting on the experience of all of you to get some help :)

I am working on a C# project and my program has to start different other applications, one by one. When an application finishes it's execution, I want to popup a message box and some other things that are not important. The applications are called from within a thread with a process, and I use an EventHandler to be called after the process finishes, just like some sort of callback function.

This works if the application I call does not call itself other applications. If so, the EventHandler that is called after the thread exits is called prematurely. More precise, I have this problem with an installer that is built with InstallShield. After the progress bar that initializes that installation, the "doneThread" EventHandler is called.

I want "doneThread" to be called when the installation finishes, not after the progress bar and installation initialization is complete.

I hope I make myself clear.. Can anyone help me on this topic?

private string commandName;
private void Begin()
        {
            StartProcess("C:\dummy_app.exe");
        }
....

public bool StartProcess(string FileName)
        {
            try
            {
               commandName = FileName;

                myThread = new Thread(this.threadWork);
                myThread.Start();

                return true;
            }
            catch (Exception)
            {
                MessageBox.Show("BlaBla");
                return false;
            }
        }

private void threadWork()
        {
            EventHandler eh = new EventHandler(doneThread);

            try
            {   
                Process p = Process.Start(commandName);
                p.WaitForExit();
            }
            catch (Exception)
            {
                MessageBox.Show();
            }

            try
            {
                //invoke doneThread when installer exits
                // this is where I do my post-exit things
                this.Invoke(eh);
            }
            catch (Exception)
            {
            }
        }

private void doneThread(object sender, EventArgs e)
        {
           ...
           // unimportant
        }

Thanks a lot for reading it all to this point :)

Comments
1st thread with [code] tags. Good question.

You can set the EnableRaisingEvents property on the Process
instance to true, and then subscribe to the Exited event.

This article has been dead for over six months. Start a new discussion instead.