当girdview填充时,我的装载将被冻结 [英] My loading will be freezed when girdview is filling

查看:58
本文介绍了当girdview填充时,我的装载将被冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview,加载数据源需要一些时间。我想为它做一个加载,我尝试了后台工作者和异步等待和task.factory.startnew和task.run和...但是当datagridview填充我的加载gif将被冻结。



我该怎么办?



我搜索了很多,我看到了一个文字。他们说我们只有一个ui线程,当一个人正在工作时,另一个会冻结是正确的吗?



例如,这就像gif正在加载的情况一个richtextbox正在填充:

pictureBox1将在我们处于巨大功能时冻结





i have a datagridview and it take times to load it datasource . i want to make a loading for it and i tried background workers and async await and task.factory.startnew and task.run and ... but when datagridview is filling my loading gif will be freezed.

what should i do?

i googled a lot and i saw a text. they said we have just one ui thread and when one is working another will be freezed is that correct?

for example, it is like a situation that gif is loading and a richtextbox is filling:
pictureBox1 will be freezed when we are in huge function


private  void  button1_Click (object sender, EventArgs e)
       {

         Task.Factory.StartNew(()=>;
         {
             huge();
         }
             );

       }







private void huge()
        {
            richTextBox1.Invoke(new EventHandler(delegate
                {

              
                        for (int i = 0; i < 10000; i++)
                        {
                            richTextBox1.Text += "s";
                        }

                }));
            pictureBox1.Visible = false;
        }





我的尝试:



i尝试后台工作者和async等待和task.factory.startnew和task.run和...但是当datagridview填充时,我的加载gif将被冻结。



What I have tried:

i tried background workers and async await and task.factory.startnew and task.run and ... but when datagridview is filling my loading gif will be freezed.

推荐答案

您尝试在网格中加载的记录数是多少?



您是否考虑过一次分页数据,如10或20条记录?



我认为在这种情况下,加载数据不需要创建新闻主题。



同样来自代码发布:'richTextBox1.Text + =s;'你应该使用StringBuilder来完成这样的任务...代码就是每次都会创建一个新的字符串。
What is the number of records that you are trying to load in grid?

Did you consider paging the data like 10 or 20 records at a time?

I think in this case the loading of data should not require creating news threads.

Also from code posted: 'richTextBox1.Text += "s";' you should use StringBuilder for such task...code as is will create a new string each time.


你需要看看在您的工作线程和UI线程之间进行编组。您的示例的工作方式是 richtext 无论如何都将填充UI线程。当然, richtext 的更新需要在UI线程上进行,而不是在获取数据(for循环)的工作中进行。最后, picturebox 的可见属性的设置也需要编组回UI线程。



你的巨大方法就变成了:



You need to look at marshalling between your worker thread and the UI thread. The way your example would work is that the richtext will fill on the UI thread anyway. Granted, the updates of the richtext need to occur on the UI thread, but not the work of getting the data (the for loop). And finally, the setting of the picturebox's visible property needs to be marshalled back to the UI thread also.

Your huge method then becomes something like:

for (int i = 0; i < 10000; ++i) {
    if (richTextBox1.InvokeRequired) {
       richTextBox1.BeginInvoke((MethodInvoker) delegate { richTextBox1.Text = richTextBox1.Text + "s"; });
    }
}





您可能希望加快UI线程上附加的操作也是RichTextbox的s。在huge()的开头获取文本的初始值,在每个循环上附加s,然后你的委托变为 {richTextBox1.Text = text; }



在UI上可以做的工作越少,响应就越快。



You'd probably want to speed up the action on the UI thread of appending the "s" to the RichTextbox as well. Get the initial value of the text at the start of huge(), append "s" to that on each loop, and then your delegate becomes { richTextBox1.Text = text; }.

The less work you can do on the UI, the more responsive it will become.


这篇关于当girdview填充时,我的装载将被冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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