Forum: VB.NET |
Thema:
Re: How to know that a thread has finshed. |
Von:
Theo Driemann (
20.04.2006 21:28) |
Dear Christian,
Maybe you have time to answer the my last 2 question.
First, the stop method wasn't working, until I declared Dim fs As New clsSearchFiles.FileSearcher(_StartupDir, New String() {SearchString}) Global. Afterwards I worked.
I made a copy of my final code. I tried to make a seperate CLASS, not running in the main of the form. Everthing is working fine except 2 things
First, when I try to write the currently browsed directoy to the label in the form nothing happend, no indication of the current directory and no error occured.
Sub GetError(ByVal sender As FileSearcher, ByVal currentDirectory As String)
Main.lbl_Status.Text = currentDirectory
End Sub
=> Do you know, why there is no indication of the directory in the label.
Second, how can I request the search result
Private Sub SearchFilesCallback(ByVal ar As IAsyncResult)
RaiseEvent Complete(Me)
End Sub
from my Main CLASS ?
Public Class Main
Dim FS As clsSearchFiles
Private Sub but_Dialog_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles but_Dialog.Click
Dim StartupDir As String = "C:\"
Dim SearchString As String = "'*.mpeg','*.mpg','*.avi'"
clsSearchFiles.FileSearcher._Cancel = False
FS = New clsSearchFiles(StartupDir, SearchString)
End Sub
Private Sub but_Cancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles but_Cancel.Click
clsSearchFiles.FileSearcher._Cancel = True
End Sub
End Class
Imports System.Collections.ObjectModel
Public Class clsSearchFiles
Public Sub New(ByVal _StartupDir As String, ByVal _SearchString As String)
Dim fs As New clsSearchFiles.FileSearcher(_StartupDir, New String() {"*.mpeg", "*.ratdvd", "avi"})
AddHandler fs.Progress, AddressOf Progress
AddHandler fs.Complete, AddressOf Complete
AddHandler fs.GetError, AddressOf GetError
fs.Start()
End Sub
Protected Overrides Sub finalize()
'Code
MyBase.Finalize()
End Sub
Sub Progress(ByVal sender As clsSearchFiles.FileSearcher, ByVal currentDirectory As String)
REM Console.WriteLine("Progress: {0}", currentDirectory)
Main.lbl_Status.Text = currentDirectory
End Sub
Sub Complete(ByVal sender As clsSearchFiles.FileSearcher)
Console.WriteLine("Complete: Found {0} files.", sender.Results.Count)
End Sub
Sub GetError(ByVal sender As FileSearcher, ByVal currentDirectory As String)
Main.lbl_Status.Text = currentDirectory
End Sub
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 currentError As String)
Public Shared _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(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, Dir)
End Try
Dim sfd As New SearchFilesDelegate(AddressOf SearchFiles)
sfd.BeginInvoke(Me.Searchroot, AddressOf SearchFilesCallback, Nothing)
End Sub
Public Sub New()
End Sub
Sub SearchFiles(ByVal dir As String)
If FileSearcher._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, dir)
End Try
Next
Try
For Each path As String In IO.Directory.GetDirectories(dir)
SearchFiles(path)
Next
Catch e As Exception
RaiseEvent GetError(Me, dir)
End Try
End Sub
Private Sub SearchFilesCallback(ByVal ar As IAsyncResult)
RaiseEvent Complete(Me)
REM StrgFound.AddRange(Me._Results)
For Each result As String In Me.Results
Console.WriteLine(result)
Next
MsgBox("Complete")
End Sub
Public Sub BreakBrowseing(ByVal StopSearch As Boolean)
FileSearcher._Cancel = StopSearch
End Sub
End Class
End Class
mfg Theo
Betreff |
Von |
Datum |
|
|
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!