Run two processes simultaneously in vb.net windows form. Multithreaded operation.

Hi

I would be grateful for your help on the following issue.

I have a windows form which has a search facility and this can sometimes take up to 10 seconds depending on the type of search being carried out. Therefore I want to update the user with a label that continually changes from one message to the other while the search is being carried out. For example after clicking the search button the label might display "Working...." for two seconds and then go to "Please wait.....". Also, just for the effect, I want the search to take a minimum of 3 seconds and the label display to change every second.

Here is where I am with code....
How could I modify the following code to fit my criteria.

Private dt as DataTable
Sub SearchDB()
        dt= Search(Surname)    'ASSUME SEARCH METHOD ALWAYS RETURNS A RESULT.
        Me.GridViewTest.AutoGenerateColumns = False
        Me.GridViewTest.DataSource = dt
    End Sub

I have the following which successfully changes the label

Private Sub Working()
        While dt.rows.count < 1
            Me.lblStatus.Text = "Searching, please wait..."
            Thread.Sleep(1000)
            Application.DoEvents()
            Me.lblStatus.Text = "Accessing database...."
            Thread.Sleep(1000)
            Application.DoEvents()
            Working()
        End While
    End Sub

Or if this code is too rubbish to even start with please provide your own example of how to do the same thing. Please for the example code, assume search method always returns a result of at least 1 record.

Your help would be much appreciated.

Thanks again.

Paul
LVL 2
jazz__manAsked:
Who is Participating?
 
nicksbellConnect With a Mentor Commented:
Try this.

Note, you need to import System.Threading for the following code.
Private dt as DataTable
Private blnFound as boolean = False
 
Sub Search
    Dim thr as New Thread(AddressOf SearchDB)
    thr.start()
    Working()
End Sub
 
Sub SearchDB()
    dt= Search(Surname)
    blnFound = True
End Sub
 
 
Private Sub Working()
    While Not blnFound
        Me.lblStatus.Text = "Searching, please wait..."
        Thread.Sleep(1000)
        Application.DoEvents()
        Me.lblStatus.Text = "Accessing database...."
        Thread.Sleep(1000)
        Application.DoEvents()
        Working()
    End While
    Me.GridViewTest.AutoGenerateColumns = False
    Me.GridViewTest.DataSource = dt
End Sub

Open in new window

0
 
DabasCommented:
Hello jazz__man,

Your title implies that you are using multithreading, but your code does not show it.
Where is the Sub Working being called from?
How does Search fill the data table?
Are you using an asynchronous call anywhere?

Regards,

Dabas
0
 
jazz__manAuthor Commented:
Cool.

Cheers.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.