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

Comments

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?

Square One available on the Android market

This is just a short post to let you know that a first version of the Android app I’ve been working on for the last couple of weeks is available on the Android market. The app is called Square One and it’s a simple bassline synthesizer. It’s free so try it out and let me know what you think of it, but be prepared it’s still an early version. I hope to add more features in the next few months and maybe build something that can be used to create real music.The lower part of the screen contains the sequencer controls that can be used to program your own bass lines. On the left is a four by four grid of buttons where you can select a step in the sequence. On the right you can select the note to be played on that step. When you’re done you can press Start and the sequence starts playing. The knobs on the top can be used to control a couple of parameters from the synthesizer engine that creates the sound. You can control the cutoff frequency and resonance of the low-pass filter, attack and …

Building Android projects with Jenkins, Ant and Mercurial

I have recently set up a Jenkins build server for my Android projects hosted on Bitbucket. It’s not difficult but there are a couple pitfalls and the information on how to do this isn’t available from one single place so I decided to document the process and put up the information over here. Maybe other people will benefit from having a step-by-step guide too.