zoukankan      html  css  js  c++  java
  • WinForm,在另一个线程中更新Form中的数据(转)

    Form本身有线程,但对于一些耗时的操作,我们不希望在Form的线程中进行,因为会导致Form线程阻塞,产生假死的现象。

    其他线程中操作Form中的控件,总出现“线程间操作无效: 从不是创建控件的线程访问它”,如何解决呢?

    很简单,利用委托。

    比如:

    xForm上有dataGridView1控件,xForm提供updateView()方法,updateView()中需要直接或间接地更新dataGridView,updateView()方法可能会在别的线程中调用。

    private delegrate void delegrateUpdateGridView();

    private void updateView()

    {

        if(dataGridView1.InvokeRequired){   //xForm之外的线程调用updateView,则执行if分支

            Invoke(new delegrateUpdateGridView(updateGridView));   //委托给xForm线程调用

        }

        else{

            updateGridView();    //如果xForm线程调用updateView,则执行else分支

        }

    }

    private updateGridView()   //实现对dataGridView1的更新

    {

        ....更新dataGridView1的代码

    }

  • 相关阅读:
    jsp登录显示
    flask 分页
    module pip has no attribute main
    html active属性
    Centos7配置
    python爬虫笔记----4.Selenium库(自动化库)
    python爬虫笔记
    python报错记录
    mybatis逆向文件
    python简单验证码
  • 原文地址:https://www.cnblogs.com/xihong2014/p/10926774.html
Copyright © 2011-2022 走看看