添加数千行时应用程序变得无响应 [英] Application Becomes Nonresponsive While Adding Thousands of Rows

查看:135
本文介绍了添加数千行时应用程序变得无响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows窗体应用程序,该应用程序创建一个后台线程来搜索文件并将匹配的文件名添加到DataGridView.后台线程使用BeginInvoke将行添加到DataGridView,如果后台线程不返回数千个结果,则一切正常.即使后台线程处理了数千个文件,该UI仍可以正常工作,除非它尝试对DataGridView进行几千行的处理.

I have a windows forms application which creates a background thread to search in files and adds matching file names to a DataGridView. Background thread uses BeginInvoke to add rows to the DataGridView and everything works fine if the background thread does not return thousands of results. The UI works perfectly fine even if the background thread processes thousands of files, unless it tries to a a few thousands of rows to the DataGridView.

我尝试一一添加行,然后对其进行缓冲并一次添加100行.但是应用程序UI始终无响应.后台进程的工作速度比UI线程快,并且总是有新行要添加到DataGridView中.

I tried adding rows one by one, and buffering them and adding 100 rows at once. But the application UI become nonresponsive all the time. The background process works faster than the UI thread and there are always new rows to add to the DataGridView.

我什至尝试了Application.DoEvents(在UI线程中),但也没有帮助.

I even tried Application.DoEvents (in the UI thread) but it did not help either.

有什么想法吗?我可以在不增加后台线程延迟的情况下进行修复吗?

Any ideas? Can I fix it without adding a delay to the background thread?

预先感谢

推荐答案

最简单的解决方案似乎是使用Invoke而不是BeginInvoke添加行.它会稍微减慢后台线程的速度,但是可以防止UI变得无响应. 因为您没有在后台线程中添加预定义的延迟,所以我认为可以稍微降低后台线程的速度只是为了让UI线程赶上来,这是可以接受的. 在使用BeginInvoke时,使用SuspendLayout和ResumeLayout也无济于事. 我还没有尝试使用共享行的虚拟模式,但这可能也会有所帮助.

It looks like using Invoke instead of BeginInvoke to add rows is the easiest solution. It slows down the background thread a little bit but it prevents the UI to become nonresponsive. Since you don't add a predefined delay to the background thread I think it may be acceptable to slow the background thread a little bit only to let the UI thread catch up. Using SuspendLayout and ResumeLayout did not help either while using BeginInvoke. I did not try virtual mode with shared rows yet but that may help too.

这篇关于添加数千行时应用程序变得无响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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