zoukankan      html  css  js  c++  java
  • RichTextBox的线程安全问题

    CSDN上的一个问题,
    http://community.csdn.net/Expert/topic/4345/4345733.xml?temp=.4753534

    网上的有关讨论如下,

    jfo's coding: Whidbey help for multithreading woes: CheckForIllegalCrossThreadCalls

    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 particular timer is the wrong choice for updating Windows Forms UI, as it calls back 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 CheckForIllegalCrossThreadCalls. 

    Control.CheckForIllegalCrossThreadCalls 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 would 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)

    Obviously, 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.CheckForIllegalCrossThreadCalls to false.  (You can also do this while debugging in VS by using the Immediate Window and typing in Control.CheckForIllegalCrossThreadCalls = false).

  • 相关阅读:
    【java多线程】队列系统之说说队列Queue
    【传输协议】什么是CA证书
    5.1 javassist基本使用
    第四章 dubbo内核之aop源码解析
    第三章 dubbo内核之ioc源码解析
    2.2 dubbo-spi源码解析
    2.1 jdk-spi的实现原理
    第一章 第一个dubbo项目
    第零章 dubbo源码解析目录
    macOS Sierra10.12.5 显示允许任何来源
  • 原文地址:https://www.cnblogs.com/s5689412/p/260646.html
Copyright © 2011-2022 走看看