我的程序没有响应 [英] my program not responding

查看:72
本文介绍了我的程序没有响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手这是代码我编程选择路径查看ricttextbox中的所有文件名

当我测试我选择 Folder1(Folder1有40个文件)我的程序完美运行然后我选择 Folder2(folder2有8,318个文件,307个文件夹)然后我的程序< b>没有回应停止工作

为什么会这样?

是另一种方法吗?

@path是selectpath from folderBrowserDialog

C#,windows表单应用程序

我是c的新手#

感谢提前!

i'm new to C# this is the code i program select path and see all file name in ricttextbox
when i test i select Folder1 (Folder1 have 40 files) my program works perfectly then i select Folder2 (folder2 have 8,318 Files, 307 Folders) then my program not responding stoped working
Why is that?
Is another way to do that?
@path is selectedpath from folderBrowserDialog
C# , windows form application
i'm new to c#
THANK IN ADVANCE !

string[] filePaths = Directory.GetFiles(@path, "*", SearchOption.AllDirectories);
            foreach (var item in filePaths)
            {
                var r = item.Substring(item.LastIndexOf(@"\") + 1);
                richTextBox1.Text = richTextBox1.Text + r + Environment.NewLine;
            }

推荐答案

这总是很慢。为什么?因为你将字符串连接8318次,并且每次生成一个足够大的全新字符串来容纳整个新字符串,然后将其转储到RichTextbox中,每次都会进行更多处理。



第一个改进:使用StringBuilder:它更高效,因为它不需要每次都重新创建,然后在循环结束时只设置一次RichTextbox内容。 br />
将我的时间减少到23毫秒,获得33,000个文件...



如果不够快有办法进一步改进,但我怀疑这就足够了。
That is always going to be slow. Why? Because you are concatenating strings 8318 times, and each time generates a whole new string large enough to hold the whole new string, and then dumping it into a RichTextbox which will do more processing each time.

First improvement: use a StringBuilder instead: it is a lot more efficient as it doesn't need to be created anew each time, and then set the RichTextbox content just once, at the end of the loop.
That reduced the time to 23 milliseconds on mine, for 33,000 files...

If that isn't fast enough there are ways to improve it further, but I suspect that will be enough.


这篇关于我的程序没有响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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