Ted Smith
Ted Smith

Reputation: 11

Stripping RTF Formatting from text causes handle error with Richtextbox

I have two separate C# ASP.NET programs running on the same server. Each of them uses the Richtextbox control in their respective business layer dll's to strip RTF formatting from text stored in the database as such.

var rtf = new RichTextBox {Rtf = itemWeb.RTF_DESCRIPTION};
                    item.WebDescription = rtf.Text;

The problem is, when both programs execute it often happens where this line fails(on both programs) with the following error.

[Win32Exception (0x80004005): Error creating window handle.]

These programs do not share any code or dll's whatsoever. The only thing in common is the technique used to strip the formatting and the fact that they are on the same server.

Is there a known issue using the Richtextbox this way? I didn't write the code, but it seems non-standard to use a UI element in a dll, even though this is the common solution when searching for how to strip formatting.

Ideally, I would find a solution without using the Richtextbox. I found one using the regex that comes close, but does not guarantee that 100% of the formatting will be stripped. Any explanations as to why this is happening or any workarounds will be appreciated.

Thanks!

Upvotes: 1

Views: 1602

Answers (1)

Isaac
Isaac

Reputation: 51

I started getting the same error recently with a method in a static class that converts from RTF to Text.

I tracked it down to the RichTextBox not being properly disposed (or possibily quickly enough) even though the context the RichTextBox was in the method (not global).

If your code doesn't get executed a lot, this might not be the same problem.

It can be reproduced by coding a test care that run through the conversion 30,000+ times. Implementing a using clause solved the problem.

using (System.Windows.Forms.RichTextBox rtBox = new System.Windows.Forms.RichTextBox())
{
    rtBox.Rtf = str;
    str = rtBox.Text; // convert the RTF to plain text.
}

This worked but it is pretty slow. It would be nice to be able to do this without having to create a control, but that's Microsoft's official advice for RTF conversion.

Upvotes: 5

Related Questions