RichTextBox thread security issues

Source: Internet
Author: User
Tags call back
A problem on csdn,
Http://community.csdn.net/Expert/topic/4345/4345733.xml? Temp =. 4753534.

The online discussion is as follows,

Jfo's coding: Whidbey help for multithreading woes: checkforillegalcrossthreadcils

A somewhat common programming error is to directly call back onto a piece of UI when you're on the wrong thread.

Consider this seemingly innocuous piece of code:

System. Timers. Timer T = new system. Timers. Timer ();

Public form1 (){
Initializecomponent ();
T. elapsed + = new system. Timers. elapsedeventhandler (timer_elapsed );
T. Enabled = true;
}

Void timer_elapsed (Object sender, system. Timers. elapsedeventargs e ){
This. richtextbox1.text + = "whoops I'm on the wrong thread! ";
}

Reading up about the various timers available in the framework, this particle timer is the wrong choice for updating Windows Forms UI, as it callback on a different thread. (Side Note: See invokerequired, invoke, begininvoke for the correct way to update the RichTextBox from another thread ).

Because the RichTextBox is NOT thread-safe, it can sometimes trip over its own editstreamproc-(putting callstack here as this seems to be a somewhat common problem folks run into in V1 ).

System. nullreferenceexception: object reference not set to an instance of an object.
At system. Windows. Forms. RichTextBox. editstreamproc (intptr dwcookie, intptr Buf, int32 CB, int32 & transferred)
At system. Windows. Forms. unsafenativemethods. callwindowproc (intptr wndproc, intptr hwnd, int32 MSG, intptr wparam, intptr lparam)
At system. Windows. Forms. nativewindow. defwndproc (Message & M)
At system. Windows. Forms. Control. defwndproc (Message & M)
At system. Windows. Forms. Control. wndproc (Message & M)
At system. Windows. Forms. textboxbase. wndproc (Message & M)
At system. Windows. Forms. RichTextBox. wndproc (Message & M)
At system. Windows. Forms. controlnativewindow. onmessage (Message & M)
At system. Windows. Forms. controlnativewindow. wndproc (Message & M)
At system. Windows. Forms. nativewindow. Callback (intptr hwnd, int32 MSG, intptr wparam, intptr lparam)

Unfortunately, this callstack is not helpful for folks trying to figure out what exactly happened to their RichTextBox. in Whidbey, we 've added a new debugger-only feature: a static property on control called checkforillegalcrossthreadcils.

Control. checkforillegalcrossthreadcils is defaulted to true when starting a Windows Forms Application under the debugger.

If you run the same application under the debugger in Whidbey, an exception wocould be thrown the moment the control. Handle property is accessed from the wrong thread:

Control 'richtextbox1' accessed from a thread other than the thread it was created on.

(Note: You'll need open debug-> exceptions menu item and check off Common Language Runtime exceptions when thrown)

Obviusly, there's no great way for Windows Forms to catch 100% the potential problems with multithreading, but I think it's a great start. if this happens to pose a compatibility problem for your program, you can set control. checkforillegalcrossthreadcallto false. (You can also do this while debugging in vs by using the immediate window and typing in control. checkforillegalcrossthreadcils = false ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.