Form in another thread

shers

Well-known member
Joined
Aug 12, 2007
Messages
86
Programming Experience
1-3
Hi,

How do I run a form that has a progress bar on a separate thread from another form?

Thanks
 
You'll need to provide a bit more information. It might be appropriate to create the form with the ProgressBar on the UI thread or maybe on the secondary thread. The question is, do you want the user to be able to do other things in the UI while the background task for which you're showing progress is running?
 
Sorry for the minimal information. Actually, there is a function running behind to populate a Grid View which takes some time. So the user should see this form with a progress bar to know that it is populating and display a text "Finished" when it's done.

Thanks
 
In that case I would suggest that your best option would be:

1. Display a modal dialogue.
2. Display a marquee progress bar in that dialogue.
3. Start a BackgroundWorker in the dialogue.
4. In the DoWork event handler, retrieve the data and expose it via a form property.
5. In the RunWorkerCompleted event handler, close the dialogue.
6. Back in the main form, get the data from the dialogue and bind it to the grid.

This might sound complicated but it's very, very simple. The main form would look something like this:
Using dialogue As New SomeForm
    dialogue.ShowDialogue()
    Me.DataGridView1.DataSource = dialogue.Data
End Using
while the dialogue might look something like this:
Private _data As DataTable

Public ReadOnly Property Data() As DataTable
    Get
        Return _data
    End Get
End Property

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.BackgroundWorker1.RunWorkerAsync()
End Sub

Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Using connection As New SqlConnection("connection string here"),
          command As New SqlCommand("SQL query here", connection)
        connection.Open()

        Using reader As SqlDataReader = command.ExecuteReader()
            Me._data = New DataTable()
            Me._data.Load(reader)
        End Using
    End Using
End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    Me.Close()
End Sub
You don't need any notification that it's finished because the ProgressBar is there to indicate that the operation is in progress so the fact that the ProgressBar disappears and the data appears should be notification enough.
 
Back
Top