Working with threads

Knvn

Well-known member
Joined
Dec 30, 2009
Messages
45
Programming Experience
Beginner
Public Class ThreadSearch
Inherits frmStDetails 'Windows form which contains my control

Public Event finishedSearching()

Sub Tsearch()

Try
settings.MoveFirst()
Catch ex As Exception

End Try

'checking filter
If Not Depmt.SelectedIndex = -1 Then
'It's to check only in perticular department
Call openSRS(Depmt.Text, isName)
Call loadSRS()
srs.Close()
Else
While Not settings.EOF
'It check's in all the department
Call openSRS(settings.Fields(1).Value, isName)
Call loadSRS()
srs.Close()
settings.MoveNext()
End While
End If

If Not found Then
ListBox1.Items.Add(search.Text & " is Not found")
ListBox1.Enabled = False
End If

RaiseEvent finishedSearching()

End Sub

End Class

The above code is my thread class.

Private Sub Search_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Search.TextChanged

Dim ts As New ThreadSearch
Dim t1 As New System.Threading.Thread(AddressOf ts.Tsearch)

If t1.IsAlive Then t1.Abort()
AddHandler ts.finishedSearching, AddressOf ts.Tsearch
t1.Start()

End Sub

This is the procedure I used to start the thread. It doesn't generates error but the problem is 'IT DOES NOTHING'.
Please help me.......
 
Last edited:
I think a BackgroundWorker may prove to be an easier implementation, and has methods for reporting progress and worker completed. It is a drop in component, and there is plenty of documentation out there.
 
Thank u for ur reply. Yet I didn’t know how to use BackgroundWorker.
If possible please give me a sample working code which uses a ‘thread’, so that I can properly understood how it works.
 
I tried backgroundworker, this is my code:

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

If Not Depmt.SelectedIndex = -1 Then
Call openSRS(Depmt.Text, isName)
Call loadSRS()
srs.Close()
Else
While Not settings.EOF
Call openSRS(settings.Fields(1).Value, isName)
Call loadSRS()
srs.Close()
settings.MoveNext()
End While
End If

If Not found Then
ListBox1.Items.Add(Search.Text & " is Not found")
ListBox1.Enabled = False
End If

End Sub

Now I get the following error: on the line 'If Not Depmt.SelectedIndex = -1 Then'

Cross-thread operation not valid: Control 'Depmt' accessed from a thread other than the thread it was created on.
 
That is exactly one of the conveniences of the BackgroundWorker component, that you in most cases do not have to resort to invoking delegates yourself. Code called in ProgressChanged and RunWorkerCompleted event handlers are safe to interact with UI thread controls.
  • If you need to pass info for the worker thread to process this can be done passing argument to RunWorkerAsync method.
  • To pass info to and raise the ProgressChanged event use ReportProgress method, WorkerReportsProgress property must be set to allow progress.
  • To pass info to RunWorkerCompleted event use e.Result from DoWork event handler, RunWorkerCompleted event is raised when DoWork is complete.
BackgroundWorker Class (System.ComponentModel)
 
Thank you for both.
I red JMC's Explaination that was really good, I found a solution for my problem but still I have a small problem i.e the code I written below generates a compile time error.

Code:

VB.NET:
 Private Delegate Function DlgtAddListItem(ByVal item As String)

    Private Sub addListItem(ByVal item As String)
        If Me.ListBox1.InvokeRequired Then
            Me.ListBox1.Invoke(New DlgtAddListItem(AddressOf addListItem), item) 'It generates error in this line
        Else
            Me.ListBox1.Items.Add(item)
        End If
    End Sub

Error: Method 'Private Sub addListItem(item As String)' does not have a signature compatible with delegate 'Delegate Function DlgtAddListItem(item As String) As Object'.

I wrote the below code to get the vale from combobox (i.e SelectedIndex), It works fine.

VB.NET:
    Private Delegate Function DlgtGetSelectedIndex() As Int16

    Private Function getSelectedIndex() As Int16

        If Me.Depmt.InvokeRequired Then
            Return CShort(Me.Depmt.Invoke(New DlgtGetSelectedIndex(AddressOf getSelectedIndex)))
        Else
            Return Depmt.SelectedIndex
        End If

    End Function
 
The delegate must have same signature as the method called, ie method signature:
VB.NET:
Sub Method(value As String)
means delegate declaration:
VB.NET:
Delegate Sub MethodHandler(value As String)
You can basically put the work 'Delegate' in front and give it a different name and you got it.
 
Thank you JohnH, I found the solution myself. Actually the mistake is, I declared the delegate as 'Function' and method as 'Sub'. Now it works fine.
 
Back
Top