Forum: VB.NET |
Thema:
Re: How to know that a thread has finshed. |
Von:
Christian Hehtke (
16.04.2006 23:36) |
Hi Theo,
i would prefer a class like this you have a progress and you have a complete event and you can cancel the operation.
i did not test the cancel method ;)
hope it works for you
Imports System.Collections.ObjectModel
Module Module1
Sub Main()
Dim fs As New FileSearcher("c:\windows\system32", New String() {"*.exe", "*.dll"})
AddHandler fs.Progress, AddressOf Progress
AddHandler fs.Complete, AddressOf Complete
fs.Start()
Console.Read()
End Sub
Sub Progress(ByVal sender As FileSearcher, ByVal currentDirectory As String)
Console.WriteLine("Progress: {0}", currentDirectory)
End Sub
Sub Complete(ByVal sender As FileSearcher)
Console.WriteLine("Complete: Found {0} files.", sender.Results.Count)
End Sub
End Module
Class FileSearcher
Public Event Progress(ByVal sender As FileSearcher, ByVal currentDirectory As String)
Public Event Complete(ByVal sender As FileSearcher)
Private _Cancel As Boolean = False
Private _SearchRoot As String
Public Property Searchroot() As String
Get
Return _SearchRoot
End Get
Set(ByVal value As String)
_SearchRoot = value
End Set
End Property
Private _SearchPattern() As String
Public Property SearchPattern() As String()
Get
Return _SearchPattern
End Get
Set(ByVal value() As String)
_SearchPattern = value
End Set
End Property
Private _Results As New List(Of String)
Public ReadOnly Property Results() As ReadOnlyCollection(Of String)
Get
Return New ReadOnlyCollection(Of String)(_Results)
End Get
End Property
Public Sub New()
End Sub
Public Sub New(ByVal searchRoot As String, ByVal pattern() As String)
Me.Searchroot = searchRoot
Me.SearchPattern = pattern
End Sub
Private Delegate Sub SearchFilesDelegate(ByVal dir As String)
Public Sub Start()
If Not IO.Directory.Exists(Me.Searchroot) Then
Throw New IO.DirectoryNotFoundException(Me.Searchroot)
End If
Dim sfd As New SearchFilesDelegate(AddressOf SearchFiles)
sfd.BeginInvoke(Me.Searchroot, AddressOf SearchFilesCallback, Nothing)
End Sub
Sub SearchFiles(ByVal dir As String)
If Me._Cancel Then Exit Sub
RaiseEvent Progress(Me, dir)
For Each pattern As String In Me.SearchPattern
Me._Results.AddRange(IO.Directory.GetFiles(dir, pattern))
Next
For Each path As String In IO.Directory.GetDirectories(dir)
SearchFiles(path)
Next
End Sub
Private Sub SearchFilesCallback(ByVal ar As IAsyncResult)
RaiseEvent Complete(Me)
End Sub
Public Sub [Stop]()
Me._Cancel = True
End Sub
End Class
greetings
Betreff |
Von |
Datum |
|
  |
Re: How to know that a thread has finshed.
Dear Christian,<br>Thank your very much for your effort to help me.<br><br>I have made another Project to test your code. It seems, your code is working fine.<br><br>In course of the next week I'm going to try... |
 |
 |
 |
|
|
Theo
Driemann
|
17.04.2006 00:54 |
|
  |
Re: How to know that a thread has finshed.
Dear Christian,<br><br>It is hard for me to follow the code you wrote, but it still works and I think it is even faster than my one. I added some lines, to catch errors when the program tries to access... |
 |
 |
 |
|
|
Theo
Driemann
|
17.04.2006 18:39 |
|
|
G.
Guest
|
18.04.2006 08:56 |
|
  |
Re: How to know that a thread has finshed.
Dear Christian,<br>Maybe you have time to answer the my last 2 question.<br><br>First, the stop method wasn't working, until I declared Dim fs As New clsSearchFiles.FileSearcher(_StartupDir, New... |
 |
 |
 |
|
|
G.
Guest
|
20.04.2006 20:55 |
|
  |
Re: How to know that a thread has finshed.
Dear Christian,<br>Maybe you have time to answer the my last 2 question.<br><br>First, the stop method wasn't working, until I declared Dim fs As New clsSearchFiles.FileSearcher(_StartupDir, New String()... |
 |
 |
 |
|
|
Theo
Driemann
|
20.04.2006 21:28 |
|
|
G.
Guest
|
21.04.2006 09:16 |
|
  |
Re: How to know that a thread has finshed.
hi, <br><br>i think you should read a book about object orientation.<br><br>if fixed the code and dropped the class you build because i did not see any sense in it sorry. <br><br>this is a short example... i let... |
 |
 |
 |
|
|
Christian
Hehtke
|
22.04.2006 12:07 |
|
  |
Re: How to know that a thread has finshed.
Dear Christian.<br><br>Thanks a lot. Know I can use your code complete.<br><br>Your are right I have to read a book. But you remember, at the beginning of our dialog I mentoined that I am a newbie. <br><br>I'm in... |
 |
 |
 |
|
|
Theo
Driemann
|
22.04.2006 17:59 |
|
|
Antworten
Vorsicht bei der Eingabe: Die Zeichen ' oder -- sind nicht erlaubt!