zoukankan      html  css  js  c++  java
  • winform线程更新控件

    // 第一步:定义委托类型
    // 将text更新的界面控件的委托类型
    delegate void SetTextCallback(string text);
    
    //第二步:定义线程的主体方法
    /// <summary>
    /// 线程的主体方法
    /// </summary>
    private void ThreadProcSafe()
    {
      //...执行线程任务
    
      //在线程中更新UI(通过控件的.Invoke方法)
      this.SetText("This text was set safely.");
    
      //...执行线程其他任务
    }
    
    //第三步:定义更新UI控件的方法
    /// <summary>
    /// 更新文本框内容的方法
    /// </summary>
    /// <param name="text"></param>
    private void SetText(string text)
    {
      // InvokeRequired required compares the thread ID of the 
      // calling thread to the thread ID of the creating thread. 
      // If these threads are different, it returns true. 
      if (this.lblMsgTips.InvokeRequired)//如果调用控件的线程和创建创建控件的线程不是同一个则为True
      {
        while (!this.lblMsgTips.IsHandleCreated)
        {
        //解决窗体关闭时出现“访问已释放句柄“的异常
        if (this.lblMsgTips.Disposing || this.lblMsgTips.IsDisposed)
        return;
        }
        SetTextCallback d = new SetTextCallback(SetText);
        this.lblMsgTips.Invoke(d, new object[] { text });
      }
      else
      {
        this.lblMsgTips.Text = text;
      }
    }
  • 相关阅读:
    jquery mobile pages之间的跳转
    Javascript中this、prototype、constructor的理解
    javaScript的引号使用
    javaScript的事件
    我的css
    HTML5 canvas的事件
    java动态代理
    (转)面向对象的 JavaScript 编程:dojo.declare 详解
    Speex手册编解码介绍 中文翻译
    Android修改自己程序字体
  • 原文地址:https://www.cnblogs.com/zhangmo/p/13552000.html
Copyright © 2011-2022 走看看