Beware when using .NET's named pipes in a windows forms application

Yesterday a user of our .net ORM Profiler tool reported that he couldn't get the snapshot recording from code feature working in a windows forms application. Snapshot recording in code means you start recording profile data from within the profiled application, and after you're done you save the snapshot as a file which you can open in the profiler UI. When using a console application it worked, but when a windows forms application was used, the snapshot was always empty: nothing was recorded.

Obviously, I wondered why that was, and debugged a little. Here's an example piece of code to record the snapshot. This piece of code works OK in a console application, but results in an empty snapshot in a windows forms application:

var snapshot = new Snapshot();
snapshot.Record();
using(var ctx = new ORMProfilerTestDataContext())
{
	var customers = ctx.Customers.Where(c => c.Country == "USA").ToList();
}
InterceptorCore.Flush();
snapshot.Stop();
string error=string.Empty;
if(!snapshot.IsEmpty)
{
	snapshot.SaveToFile(@"c:\temp\generatortest\test2\blaat.opsnapshot", out error);
}
if(!string.IsNullOrEmpty(error))
{
	Console.WriteLine("Save error: {0}", error);
}

(the Console.WriteLine doesn't do anything in a windows forms application, but you get the idea).

ORM Profiler uses named pipes: the interceptor (referenced and initialized in your application, the application to profile) sends data over the named pipe to a listener, which when receiving a piece of data begins reading it, asynchronically, and when properly read, it will signal observers that new data has arrived so they can store it in a repository. In this case, the snapshot will be the observer and will store the data in its own repository.

The reason the above code doesn't work in windows forms is because windows forms is a wrapper around Win32 and its WM_* message based system. Named pipes in .NET are wrappers around Windows named pipes which also work with WM_* messages. Even though we use BeginRead() on the named pipe (which spawns a thread to read the data from the named pipe), nothing is received by the named pipe in the windows forms application, because it doesn't handle the WM_* messages in its message queue till after the method is over, as the message pump of a windows forms application is handled by the only thread of the windows forms application, so it will handle WM_* messages when the application idles.

The fix is easy though: add Application.DoEvents(); right before snapshot.Stop(). Application.DoEvents() forces the windows forms application to process all WM_* messages in its message queue at that moment: all messages for the named pipe are then handled, the .NET code of the named pipe wrapper will react on that and the whole process will complete as if nothing happened.

It's not that simple to just say 'why didn't you use a worker thread to create the snapshot here?', because a thread doesn't get its own message pump: the messages would still be posted to the window's message pump. A hidden form would create its own message pump, so the additional thread should also create a window to get the WM_* messages of the named pipe posted to a different message pump than the one of the main window.

This WM_* messages pain is not something you want to be confronted with when using .NET and its libraries. Unfortunately, the way they're implemented, a lot of APIs are leaky abstractions, they bleed the characteristics of the OS objects they hide away through to the .NET code. Be aware of that fact when using them :)

2 Comments

  • @Joost: no I use pure named pipes, so no WCF at all. Synchronization context is for marshaling an object to the foreground thread. Also needed in winforms apps (as there's 1 thread ;)) but not related to this problem. Good point though. SynchronizationContext is often overlooked too. :)

  • As a point of clarification, I'm near certain that win32 named pipes don't use WM_* messages as I've used named pipes from services which had no message pump, but that WinForms uses messages to implement the Begin/End async stuff.

Comments have been disabled for this content.