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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
nicksbellCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jazz__manAuthor Commented:
Cool.

Cheers.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.