Forum: VB.NET |
Thema:
Re: How to know that a thread has finshed. |
Von:
Christian Hehtke (
22.04.2006 12:07) |
hi,
i think you should read a book about object orientation.
if fixed the code and dropped the class you build because i did not see any sense in it sorry.
this is a short example... i let some tasks open
Imports System.Collections.ObjectModel
Public Class Form1
Private searcher As FileSearcher
Private Sub but_Cancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles but_Cancel.Click
searcher.Stop()
End Sub
Private Sub but_Dialog_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles but_Dialog.Click
Dim root As String = "C:\"
Dim pattern() As String = New String() {"*.xml"}
searcher = New FileSearcher(root, pattern)
AddHandler searcher.Progress, AddressOf Progress
AddHandler searcher.GetError, AddressOf [Error]
AddHandler searcher.Complete, AddressOf Complete
searcher.Start()
End Sub
Private Delegate Sub SetControlText(ByVal ctrl As Control, ByVal text As String)
Sub SetLabelText(ByVal ctrl As Control, ByVal text As String)
ctrl.Text = text
End Sub
Private Sub Progress(ByVal sender As FileSearcher, ByVal dir As String)
If Label1.InvokeRequired Then
Dim dele As New SetControlText(AddressOf SetLabelText)
Label1.Invoke(dele, New Object() {Label1, dir})
End If
End Sub
Private Sub Complete(ByVal sender As FileSearcher)
End Sub
Private Sub [Error](ByVal sender As FileSearcher, ByVal ex As Exception)
Debug.WriteLine(ex.ToString)
End Sub
End Class
Class FileSearcher
Public Event Progress(ByVal sender As FileSearcher, ByVal currentDirectory As String)
Public Event Complete(ByVal sender As FileSearcher)
Public Event GetError(ByVal sender As FileSearcher, ByVal ex As Exception)
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 _IsSearching As Boolean = False
Public ReadOnly Property IsSearching() As Boolean
Get
Return _IsSearching
End Get
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(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()
Try
If Not IO.Directory.Exists(Me.Searchroot) Then
Throw New IO.DirectoryNotFoundException(Me.Searchroot)
End If
Catch e As Exception
RaiseEvent GetError(Me, e)
End Try
Dim sfd As New SearchFilesDelegate(AddressOf SearchFiles)
sfd.BeginInvoke(Me.Searchroot, AddressOf SearchFilesCallback, Nothing)
_Cancel = False
_Results.Clear()
_IsSearching = True
End Sub
Public Sub [Stop]()
Me._Cancel = True
End Sub
Public Sub New()
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
Try
Me._Results.AddRange(IO.Directory.GetFiles(dir, pattern))
Catch e As Exception
RaiseEvent GetError(Me, e)
End Try
Next
Try
For Each path As String In IO.Directory.GetDirectories(dir)
SearchFiles(path)
Next
Catch e As Exception
RaiseEvent GetError(Me, e)
End Try
End Sub
Private Sub SearchFilesCallback(ByVal ar As IAsyncResult)
RaiseEvent Complete(Me)
End Sub
End Class
as an alternative you could use the backgroundworker component its easy to use.
mfg
Betreff |
Von |
Datum |
|
  |
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!