枚举器性能问题(需要更好的方法) [英] Enumerator Performance Issue (Needs a better method)
本文介绍了枚举器性能问题(需要更好的方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
已删除消息,请删除此消息!
message removed Please Delete this message!!
推荐答案
基于代码的第一印象:访问UI控件的属性总是很慢.由于您要遍历文件,因此我认为不必将文本等设置为控件,而应使用变量.这也适用于textbox1和... 2.由于文本框2在迭代过程中是静态的",因此将文本"属性中的值带到字符串变量中,然后在循环中使用它.
另外,由于您在代码中填充了一个列表框,因此请不要随便添加项目,而是将它们收集到一个集合中,然后在循环之后,使用ListBox.ObjectCollection.AddRange方法 [ ^ ]
加法:
我编写的代码很可能是错误的,因为我不了解代码的全部概念,但也许是以下内容:
Based on the first impression of your code: Accessing properties of an UI control is always slow. Since you''re iterating through files , in my opinion you don''t have to set the texts etc to the controls, use variables instead. This also applies to textbox1 and ...2. Since textbox 2 is ''static in during your iteration, take the value from the Text property to a string variable and use that in your loop.
Also since you fill a list box in your code, don''t add the items as you go but gather them into a collection and after the loop, use ListBox.ObjectCollection.AddRange Method[^]
Addition:
Most likely the code I wrote is wrong since I didn''t know the whole idea for the code, but perhaps something like the following:
...
Dim text1 As String
Dim text2 As String
Dim diTop As New System.IO.DirectoryInfo(ComboBox1.SelectedItem)
Try
' Enumerate all subdirectories.
For Each di In diTop.EnumerateDirectories("*")
Try
' Enumerate each file in each subdirectory.
For Each fi In di.EnumerateFiles("*", System.IO.SearchOption.AllDirectories)
text1 = getMd5Hash(fi.FullName)
If text2.Contains(text1) Then
itemList.Add(fi.FullName)
End If
Try
' Catch unauthorized access to a file.
Catch UnAuthFile As UnauthorizedAccessException
Console.WriteLine("UnAuthFile: {0}",
UnAuthFile.Message)
End Try
Next
ListBox2.Items.AddRange(itemList.ToArray())
' Catch unauthorized access to a subdirectory.
Catch UnAuthSubDir As UnauthorizedAccessException
...
这篇关于枚举器性能问题(需要更好的方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文