September 24, 2008

Process weirdness and how to solve it

I had a weird problem today. For a piece of software I'm creating I had to start an executable and then poll it periodically to see if it was still living. If it didn't respond for a certain time my program had to kill it and start over. There's a neat class in System.Diagnostics that can do this. System.Diagnostics.Process. But I found sometimes it acts a little strange.

I started out by calling the static function System.Diagnosics.Process.Start() with an executable name. This will give you a Process instance back. If the executable you've started has a main window you can then poll _processInstance.Responding to see if the thread is still alive. The main window part is important because the Process class uses the message-queue in the main window to check if the process is still responding. It does this by putting messages in the message-queue and checking if they're processed.
I used a System.Timers.Timer to check back every second Responding property was still true. If it was false my application started counting the numer of seconds it stayed false and after a minute I called _processInstance.Kill() and then _processInstance.Start(). Everything worked and everyone was happy.
The weirdness began when I started to reuse the timer. I had several of these Process instances lying around and I wanted to have them use one timer. This meant that the timer was already running when the process got started and this meant that the time between the _processInstance.Start and the first _processInstance.Responding poll could be shorter than 1 second. Suddenly most of my process instances started misbehaving. Responding was suddenly true all the time even when I hung the executables on purpose. I even created a special application for this with a screen with only one big button... Hang.
Of course this wasn't obvious to me then but after some head scratching I found that polling putting a Thread.Sleep(1000) after the _processInstance.Start() I could prevent this from happening. And polling Responding right after starting the thread reproduced the error condition in 100% of the cases. This worked:
   1: _processInstance.Start();
   2: Thread.Sleep(1000);

and this didn't:


   1: _processInstance.Start();
   2: bool responding = _processInstance.Responding();
   3: Thread.Sleep(1000);

After some more head-scratching I decided I didn't like the Thread.Sleep solution too much because I don't understand why it's working. So I went an tried out my newly-favorite website. I asked a question on StackOverflow.com. This got me some reputation points (almost got me a self-learner badge too. Only need one more vote on my own answer :-) hint! hint!) and some good answers. With Rob Coopers advice I tried:


   1: _processInstance.Start();
   2: _processInstance.WaitForInputIdle();

And this seemed to work... most of the time.

But most of the time isn't good enough. So I fired up Reflector (thanks for the good advice LaTtex!) and took a look at what was really happening. I found this code inside the Responding property-get:


   1: IntPtr mainWindowHandle = this.MainWindowHandle;
   2: if (mainWindowHandle == IntPtr.Zero)
   3: {
   4:     this.responding = true;
   5: }
   6: else
   7: {
   8:     IntPtr ptr2;
   9:     this.responding = 
  10:         NativeMethods.SendMessageTimeout(
  11:             new HandleRef(this, mainWindowHandle), 0, IntPtr.Zero, 
  12:             IntPtr.Zero, 2, 0x1388, out ptr2) != IntPtr.Zero;
  13: }

And found out that retrieving the MainWindowHandle too soon after starting the process was causing all the trouble. This is what it looks like


   1: get
   2: {
   3:     if (!this.haveMainWindow)
   4:     {
   5:         this.EnsureState(State.HaveProcessInfo | State.IsLocal);
   6:         this.mainWindowHandle = ProcessManager.GetMainWindowHandle(this.processInfo);
   7:         this.haveMainWindow = true;
   8:     }
   9:     return this.mainWindowHandle;
  10: }

It only gets the main window handle the first time. It seems calling it too soon after starting the process gives you the wrong window handle somehow and this one then gets stored during the lifetime of the process. The way to resolve this problem was by setting the haveMainWindow property to false. This can be done by calling _processInstance.Refresh(); every time you do something that uses the window handle of the process.

Another problem solved.

No comments:

Post a Comment