?
Solved

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

Posted on 2008-11-11
3
Medium Priority
?
1,097 Views
Last Modified: 2013-11-07
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
0
Comment
Question by:jazz__man
3 Comments
 
LVL 27

Expert Comment

by:Dabas
ID: 22934136
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
 
LVL 2

Accepted Solution

by:
nicksbell earned 2000 total points
ID: 22934290
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
 
LVL 2

Author Closing Comment

by:jazz__man
ID: 31515616
Cool.

Cheers.
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question