zoukankan      html  css  js  c++  java
  • WinForm中多线程操作控件

          在WinForm中的多线程中,操作控件时,会报无效操作异常,解决方法是调用System.Windows.Form.Control中BeginInvoke。
    代码如下:
    Code

    再看《CLR.via.C#.Second.Edition.Feb.2006》中是这么说,这下清楚了。
     
      
     In Windows, a window is always created by a thread, and this thread must be used to process
    all actions for the window. One reason for this is because  16-bit Windows versions were sin-
    gle-threaded operating systems, and in order to maintain backward compatibility, 32-bit and 64-
    bit versions of Windows kept the single-threaded architecture for handling window opera-
    tions such as WM_MOVE, WM_SIZE, WM_PAINT,  WM_CLOSE, etc. It's common for a Windows Forms
    application to use the asynchronous techniques explained in this chapter. However, since Win-
    dows Forms is built on top of Windows, a thread pool thread is not allowed to directly manipu-
    late a window; or more specifically, a class derived from System.Windows.Forms.Control.

        Fortunately, the System.Windows.Forms.Control class offers three methods—Invoke, Begin-
    Invoke
    , and EndInvoke-that you can call from any thread (including a thread pool thread) to
    marshal an operation from the calling thread to the thread that created the window. Inter-
    nally, Control's Invoke method calls the Win32 SendMessage method to have the window's
    thread execute a task synchronously. Control's BeginInvoke method internally calls the
    Win32 PostMessage method to have the window's thread execute a task asynchronously. If
    the calling thread wants to know when the window's thread has completed executing the
    task, it can call Control's EndInvoke method. If you don't care, this is one of the rare cases
    when you do not have to call an EndInvoke method.


  • 相关阅读:
    ExtJS Form布局
    UML系列 (六)如何提取用例技术?
    牛腩新闻发布系统 (4)验证码的生成
    DIV +CSS 系列详细教程 (一)初识
    Java、JavaScript、asp.net 、jquery 详细分析
    设计模式详细系列教程 (三)以网上购物通用的订单流程 详解状态模式
    设计模式详细系列教程 (四) 无处不在的单例模式
    SCM软件配置管理 (二) SVN管理平台搭建以及详细设置
    JAVA JDK环境变量的配置
    牛腩新闻发布系统 (5) 总结
  • 原文地址:https://www.cnblogs.com/wintersun/p/1269255.html
Copyright © 2011-2022 走看看