什么是异步加载数据到数据网格视图的最佳方式? [英] What's the best way to asynchronously load data into a Data Grid View?

查看:77
本文介绍了什么是异步加载数据到数据网格视图的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这code允许数据被加载之前,但一些形式的组件,如按钮和datagridview的本身是隐形,直到数据被加载的形式来加载。

我该如何解决这个问题?

 私人无效Form1_Load的(对象发件人,EventArgs的)
    {
        线程t =新主题(新的ThreadStart(委托()
        {
            this.Invoke(新MyDelegate(委托()
            {
                的ReadXml(路径);
                绑定();
           }));
        }));

        t.Start();
     }

    私人无效绑定()
    {
        dataGridView1.DataSource =表;
    }
 

我也有这个其他的code这工作得更好,但需要我用2个新的线程。这不可能做到这一点的最好办法,可以吗?

 私人无效Form1_Load的(对象发件人,EventArgs的)
    {
        线程t =新主题(新的ThreadStart(委托()
          {
              this.Invoke(新InvokeDelegate(委托()
              {
                  线程T2 =新主题(新的ThreadStart(委托()
                  {
                      的ReadXml(路径);
                  }));
                  t2.Start();
                  t2.Join();
                  绑定();
              }));
          }));

        t.Start();
    }
 

解决方案

如果您使用,而不是一个Invoke()的code的代表将在当前UI线程上执行的BeginInvoke(),但它不会发生,直到目前所有的UI工作的未决完成后(如当前Form1_Load的调用)。调用是一个同步调用,为什么你所需要的线程的。

 无效Form1_Load的(对象发件人,EventArgs的)
    {
        this.BeginInvoke(新MyDelegate(委托()
        {
            的ReadXml(路径);
            绑定();
        }));
    }
 

This code allows the form to load before the data is loaded but some of the components on the form such as buttons and the datagridview itself are "invisible" until the data is loaded.

How do I fix this problem?

    private void Form1_Load(object sender, EventArgs e)
    {
        Thread t = new Thread(new ThreadStart(delegate()
        {
            this.Invoke(new MyDelegate(delegate()
            {               
                ReadXml(path);
                Bind();
           }));              
        }));

        t.Start();
     }

    private void Bind()
    {
        dataGridView1.DataSource = table;
    }

I also have this other code which works better, but requires that I use 2 new threads. This can't be the best way to do this, can it?

    private void Form1_Load(object sender, EventArgs e)
    {
        Thread t = new Thread(new ThreadStart(delegate()
          {
              this.Invoke(new InvokeDelegate(delegate()
              {
                  Thread t2 = new Thread(new ThreadStart(delegate()
                  {
                      ReadXml(path);
                  }));
                  t2.Start();
                  t2.Join();
                  Bind();
              }));
          }));

        t.Start();
    }

解决方案

If you use BeginInvoke() instead of an Invoke() the code in the delegate will be executed on the current UI thread but it wont happen until after all the current UI work pending finishes (like the current Form1_Load invocation). Invoke is a synchronous call so that's why you needed the thread.

   void Form1_Load(object sender, EventArgs e)
    {
        this.BeginInvoke(new MyDelegate(delegate()
        {
            ReadXml(path);
            Bind();
        }));
    }

这篇关于什么是异步加载数据到数据网格视图的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆