Есть код ( Он не мой, нашел на одном из форумом, добавил поток. Без него программа зависала):
Основная форма:
- Код: Выделить всё
Imports System.Threading
Public Class Form1
Public trd As Thread
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
trd = New Thread(AddressOf ThreadTask)
trd.IsBackground = True
trd.Start()
End Sub
Private Sub ThreadTask()
Try
Dim rnd As New Random()
Dim searcher As New FindFiles
For i As Object = 0 To My.Computer.FileSystem.Drives.Count
For Each s As String In searcher.FindAllFiles(My.Computer.FileSystem.Drives.Item(i).ToString, "*.*")
ListBox1.Items.Add(s)
Label1.Text = s
Label2.Text = Label2.Text + 1
Thread.Sleep(150)
Next
Next
ListBox1.Items.Add("Done!")
Catch
End Try
End Sub
End Class
Класс:
- Код: Выделить всё
Class FindFiles
Public Function FindAllFiles(ByVal startPath As String, ByVal pattern As String) As List(Of String)
Dim dir As System.IO.DirectoryInfo = My.Computer.FileSystem.GetDirectoryInfo(startPath)
Return FindFile(dir, SimplePat2RegEx(pattern))
End Function
Private Function SimplePat2RegEx(ByVal pattern As String) As String
Return pattern.Replace("*", "[a-zA-Z0-9]+").Replace("?", "[a-zA-Z0-9]{1,1}")
End Function
Private Function FindFile(ByVal dir As IO.DirectoryInfo, ByVal pattern As String) As List(Of String)
Dim out As New List(Of String)
Try
For Each f As IO.FileInfo In dir.GetFiles
If IsMatch(f.FullName, pattern) Then
out.Add(f.FullName)
Debug.Print(f.FullName)
End If
Next
For Each d As IO.DirectoryInfo In dir.GetDirectories
For Each o As String In FindFile(d, pattern)
out.Add(o)
Next
Next
Catch ex As Exception
End Try
Return out
End Function
Private Function IsMatch(ByVal filename As String, ByVal pattern As String)
Dim regex As New System.Text.RegularExpressions.Regex(pattern)
Return regex.IsMatch(filename)
End Function
End Class
Все работает но, после нажатия на кнопку поиск начинается не сразу а с задержкой 20 - 30 сек. Как заставить работать сей код сразу, без задержки?
Заранее благодарю за помощь.