Skip to main content

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 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.


Popular posts from this blog

Using xUnit.Net with .Net 4.0

I’ve been using xUnit.Net for a while now. It’s just a tiny bit cleaner and slightly less abrasive than other .Net unit testing frameworks. Leaving out unnecessary stuff like [TestFixture] and shortening Assert.AreEqual to the equally clear but shorter Assert.Equal don’t seem like big improvements but when you type them several times a day tiny improvements start to add up. I also like the use of the [Fact] attribute instead of [Test]. It shifts the focus from testing to defining behavior. So how do we get all this goodness working with the Visual Studio 2010 beta?

Running a Git repository on Ubuntu using Gitosis

20I’ve been using Git for a couple of small projects that I’ve been hosting on but version control for my bigger ‘secret’ projects still runs on a windows machine with visual svn server.Now that I’m starting to use Mono for a couple of projects so I’m playing with linux more. Last week I decided to try to try out gitosis on an ubuntu server. I found out it’s pretty easy to use when you know your way around git but for a noob like me some things weren’t immediately clear. Eventually I solved most problems I ran into, so I decided to write up the steps I took to install gitosis on ubuntu 10.04

Android development resource links

I've been playing with the Android SDK and I have a growing list of bookmarks to Android dev resources for my own use. I thought the best place to keep them would be here on my blog. That way other people can benefit too. I'll keep updating this list so feel free to add suggestions in the comments.