views:

332

answers:

1

I'm using a RichTextBox control in a Windows Service to convert RTF to plain text. This method is actually mentioned by MS here: http://msdn.microsoft.com/en-us/library/cc488002.aspx

My Windows Service spawns multiple threads (typically 2 x the number of CPU cores), and each of these threads ends up instantiating a separate instance of the RichTextBox control and using it to convert RTF to plain text.

This seems to work great, except for when the service is run on machines with many cores. (4+). In these scenarios, the service will occasionally just lock up. The CPU spins at roughly 10%, and nothing happens. This will go on forever unless you kill the process. I finally managed to attach a debugger while it was happening, and it turns out it is something the RichTextBox control is doing. Looks like some internal lock that's placed when creating a new window handle.

I'm making sure that I dispose of the RichTextBox after every use. This doesn't seem to happen if I force the service to use fewer threads, but this dramatically reduces the throughput of my service on multi-core machines.

Anybody have any ideas on how to fix this, or any idea on a better way to convert RTF to plain text?

Here is the relevant portion of the stack trace of one of the threads that's stuck:

  [In a sleep, wait, or join] 
> System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.CreateHandle(System.Windows.Forms.CreateParams cp) Line 702 + 0x24 bytes C#
  System.Windows.Forms.dll!System.Windows.Forms.Control.CreateHandle() Line 5632 C#
  System.Windows.Forms.dll!System.Windows.Forms.TextBoxBase.CreateHandle() Line 1478 C#
  System.Windows.Forms.dll!System.Windows.Forms.RichTextBox.Rtf.set(string value) Line 759 C#
+1  A: 

Check this thread for a Regex example: http://stackoverflow.com/questions/188545/regular-expression-for-extracting-text-from-an-rtf-string.

There may be some open source libraries for converting Rtf to plain text (for example, http://sourceforge.net/projects/nrtftree/ - haven't tested it, but looks like you might extract the text for each Rtf node easily).

Instantiating a control in a non-gui thread simply sounds like trouble.

Groo