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的代码

    }

  • 相关阅读:
    用Total Commander for Android管理应用程序
    我的zsh简单设置
    C# Newtonsoft.Json 使用
    Wireshark 抓包 test
    C# 调用API test
    C# 委托 的语法 之一
    C# 对象初始化器 和数组初始化语法
    C 语言 数据类型长度
    vue 使用 test
    test
  • 原文地址:https://www.cnblogs.com/xihong2014/p/10926774.html
Copyright © 2011-2022 走看看