开发者

RichTextBox lifetime in static method context

Please take a look at the method depicted below

        public static string RemoveRTF(string input)
        {
            string output = input;

            RichTextBox RichTextBox1 = new RichTextBox();
            try {
                RichTextBox1.Rtf = input;
                output = RichTextBox1.Text;
            } catch (ArgumentException argExp) { 
                /*
                 * The supplied input value is not in RTF format. 
                 * Ignore.
                 */
            }
            return output;
        开发者_开发知识库}

My question is, will the above code when called several times generate a large amount of USER Objects, Handles Or GDI Objects.

The reason for asking is that I have some code which worked perfectly one day and then the next day without any code changes made stopped working with the reported error : Error creating Window Handle..

Only thing is that I cant seem to see the cause for the problem except that the callstack shows me that the error originates in the above code.

TaskManager do not reveal a large amount of USER objects or such being created, so I really do not know what is going on.


You should dispose your RichTextBox to free up any unmanaged resources.

RichTextBox1.Dispose();


or you can make one global RichTextBox and use it.

    RichTextBox RichTextBox1 = new RichTextBox();
    public static string RemoveRTF(string input)
            {

    string output = input;

    try {
                    RichTextBox1.Rtf = input;
                    output = RichTextBox1.Text;
RichTextBox1.rtf = null;
                } catch (ArgumentException argExp) { 
                    /*
                     * The supplied input value is not in RTF format. 
                     * Ignore.
                     */
                }
                return output;
            }

or use using()


The RichTextBox object only works on the UI thread. Calling this code from the background thread will throw an Exception with a message like "Error creating Window Handle.."


I also received the same error that u r facing "Error creating window handle". This issue occurs because even if we create object of RichTextBox and and set that object to null at the end of the method, it does not get disposed and so initially it works fine and then later on it start giving "error creating window handle". So instead use "Using". It will dispose object of richTextBox outside "using" context. This will solve that error.

private String RemoveRtf(String RtfScript) { string PlainText = null;

        try
        {
            if (!String.IsNullOrEmpty(RtfScript))
            {

                using (RichTextBox richTxtBox = new RichTextBox())
                {


                    richTxtBox.Rtf = RtfScript;

                    PlainText = richTxtBox.Text;
                }
            }
        }
        catch (Exception ex)
        {
            // log error here
        }
        finally
        {
            RtfScript = null;

        }
        return PlainText;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜