D
dan artuso
Hi,
I'm having a problem implementing multithreading in
an app that basically recurses through the path passed to it.
The main interface is basically an explorer knockoff and the user can
perform certain
tasks on all files and directories within their selection.
The problem is that for a little bit the main UI is responsive after spawing
the thead but continued
use of it causes both the main thread and the worker thread to freeze up. If
we don't touch the UI at all,
everything works
Note that we are not trying to update any form controls from the worker
thread!
I'll post what code I think is relevant and anyone who has any ideas, please
let us know!
Thanks
Dan
here is code in the form:
Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdStart.Click
'for this quick search, the only pattern passed willl be the PermPattern
Dim objPerms As New PermPattern
'we still need a list of patterns because the Search constructor expects
it.
Dim thisPatternList As New List(Of Pattern)
'this is for each path in the listview
Dim strPaths As New List(Of String)
Dim csitems As New List(Of CShItem)
objPerms.state = PermPattern.permissions.accessDenied +
PermPattern.permissions.noAdmins + _
PermPattern.permissions.noSystem + PermPattern.permissions.notAGroup + _
PermPattern.permissions.pathTooLong
thisPatternList.Add(objPerms)
For Each lvItem As ListViewItem In Me.lvDirs.Items
Dim csItem As New CShItem(lvItem.Text)
csitems.Add(csItem)
Next
'our search object
Dim objSearch As New Search(thisPatternList, csitems, Me)
objSearch.Start()
End Sub
Here is the code in the Search class:
'rference to calling form
Private m_clientApp As Form
'create a delegate that will call back into the main form
Private Delegate Sub ProcessResults(ByVal objSearch As Search)
'create an object for the delegate
Private m_callMainForm As ProcessResults
Constructor:
Sub New(ByVal Patterns As List(Of Pattern), ByVal cshItems As List(Of
CShItem), ByVal callingForm As Form)
m_patterns = Patterns
m_results = New List(Of Result)
m_cshItems = cshItems
m_clientApp = callingForm
'dim all possible form variables
Dim policyAudit As fPolicyAudit
Dim changeOwner As fChangeOwner
'try to cast
policyAudit = TryCast(callingForm, fPolicyAudit)
changeOwner = TryCast(callingForm, fChangeOwner)
If Not IsNothing(policyAudit) Then
m_callMainForm = AddressOf policyAudit.Processresults
End If
End Sub
Protected InnerThread As New Thread(AddressOf Me.SearchFiles)
Public Sub Start()
InnerThread.SetApartmentState(ApartmentState.STA)
InnerThread.IsBackground = True
InnerThread.Priority = ThreadPriority.BelowNormal
InnerThread.Start()
End Sub
'here is the routine that starts on a seperate thread. Basically, for each
path passed in, it calls WorkWithItems which recurses through files and
folders
Public Sub SearchFiles()
Debug.Print("SearchFiles: " &
System.Threading.Thread.CurrentThread.ManagedThreadId.ToString)
m_results.Clear()
For Each csItem As CShItem In m_cshItems
WorkWithItems(csItem)
Next
Debug.Print("about to callback")
'call back to calling form passing the Search object so results can be
processed
m_clientApp.Invoke(m_callMainForm, Me)
End Sub
I'm having a problem implementing multithreading in
an app that basically recurses through the path passed to it.
The main interface is basically an explorer knockoff and the user can
perform certain
tasks on all files and directories within their selection.
The problem is that for a little bit the main UI is responsive after spawing
the thead but continued
use of it causes both the main thread and the worker thread to freeze up. If
we don't touch the UI at all,
everything works
Note that we are not trying to update any form controls from the worker
thread!
I'll post what code I think is relevant and anyone who has any ideas, please
let us know!
Thanks
Dan
here is code in the form:
Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdStart.Click
'for this quick search, the only pattern passed willl be the PermPattern
Dim objPerms As New PermPattern
'we still need a list of patterns because the Search constructor expects
it.
Dim thisPatternList As New List(Of Pattern)
'this is for each path in the listview
Dim strPaths As New List(Of String)
Dim csitems As New List(Of CShItem)
objPerms.state = PermPattern.permissions.accessDenied +
PermPattern.permissions.noAdmins + _
PermPattern.permissions.noSystem + PermPattern.permissions.notAGroup + _
PermPattern.permissions.pathTooLong
thisPatternList.Add(objPerms)
For Each lvItem As ListViewItem In Me.lvDirs.Items
Dim csItem As New CShItem(lvItem.Text)
csitems.Add(csItem)
Next
'our search object
Dim objSearch As New Search(thisPatternList, csitems, Me)
objSearch.Start()
End Sub
Here is the code in the Search class:
'rference to calling form
Private m_clientApp As Form
'create a delegate that will call back into the main form
Private Delegate Sub ProcessResults(ByVal objSearch As Search)
'create an object for the delegate
Private m_callMainForm As ProcessResults
Constructor:
Sub New(ByVal Patterns As List(Of Pattern), ByVal cshItems As List(Of
CShItem), ByVal callingForm As Form)
m_patterns = Patterns
m_results = New List(Of Result)
m_cshItems = cshItems
m_clientApp = callingForm
'dim all possible form variables
Dim policyAudit As fPolicyAudit
Dim changeOwner As fChangeOwner
'try to cast
policyAudit = TryCast(callingForm, fPolicyAudit)
changeOwner = TryCast(callingForm, fChangeOwner)
If Not IsNothing(policyAudit) Then
m_callMainForm = AddressOf policyAudit.Processresults
End If
End Sub
Protected InnerThread As New Thread(AddressOf Me.SearchFiles)
Public Sub Start()
InnerThread.SetApartmentState(ApartmentState.STA)
InnerThread.IsBackground = True
InnerThread.Priority = ThreadPriority.BelowNormal
InnerThread.Start()
End Sub
'here is the routine that starts on a seperate thread. Basically, for each
path passed in, it calls WorkWithItems which recurses through files and
folders
Public Sub SearchFiles()
Debug.Print("SearchFiles: " &
System.Threading.Thread.CurrentThread.ManagedThreadId.ToString)
m_results.Clear()
For Each csItem As CShItem In m_cshItems
WorkWithItems(csItem)
Next
Debug.Print("about to callback")
'call back to calling form passing the Search object so results can be
processed
m_clientApp.Invoke(m_callMainForm, Me)
End Sub