Thursday, February 24, 2005
Friday, February 11, 2005
Recently I was working on a problem where I had to create new process on a click of a button. With this, I needed some callback or signal when this new process terminates. This new process was launched on a new thread. I looked into the Thread Class but there was no out of box functionality to achieve this. So I end up writing this code.
Here is the PInvoke Declaration
static extern uint WaitForSingleObject(
Creating a new Thread to start our process…
newThread = new Thread(new ThreadStart(StartNewApplication));
newThread.Name = "ClientApplication";
private void StartNewApplication()
const uint INFINITE = 0xFFFFFFFF;
//Creating a new process
Process pr = new Process();
pr.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
//Using WaitForSingleObject to wait till the threads give a
//signal. As we want to get signaled when the thread gets
//terminated, we set timeout time as "Infinite".
IntPtr HANDLE = pr.Handle;
returnValue = WaitForSingleObject(HANDLE,INFINITE);
if (returnValue == 0xFFFFFFFF)
//Getting error code...
error = Marshal.GetLastWin32Error();
//Code which we want to execute after the thread has been terminated