user1732364
user1732364

Reputation: 993

How to use threading for Loading Bar WinForm?

I am wanting to have a an infinite loop progress bar, just to give the user something to stare at during long load times. Currently the user will select a SQL query to execute, and results get displayed onto another winform that contains a gridview for the query results. My goal is to have another winform(loading form) that just has a progress bar on it that just infinitely fills to end and resets over and over until the gridview on the results form finishes rendering. I tried a background worker because i believe this will need multi threading for performance but the load form never shows up. Basically the execution plan should be this:

User clicks execute button, show progress bar on load form(infinite loop), Executes query and loads result form Closes progress bar on load form

The above execution is called from my Main form

Private Sub LoadingForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    pbSqlCall.Minimum = 0
    pbSqlCall.Maximum = 100
    pbSqlCall.Step = 10
    Dim Counter As Integer = 0
    While Counter < 100
        pbSqlCall.Increment(10)
        Counter += 10
        If Counter = 100 Then
            Counter = 0
            pbSqlCall.Value = 0
        End If
    End While
End Sub


    BackgroundWorker1.RunWorkerAsync()
    ExecuteQuery(Parameters, Queries.Item(ddlQueries.SelectedIndex))
    'Not sure how to close the form using the thread


Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    lf.ShowDialog() 'Load form
End Sub

Upvotes: 0

Views: 1530

Answers (1)

codechurn
codechurn

Reputation: 3990

You can do the following given a form called 'Loader' and a form called 'Main'

Loader:

Public Class Loader
    Private Sub Loader_Load(sender As Object, e As EventArgs) Handles MyBase.Load
       pbSqlCall.Style = ProgressBarStyle.Marquee
       pbSqlCall.MarqueeAnimationSpeed = 30
    End Sub
End Class

Main:

Imports System.Threading.Tasks
Public Class Main

    Private Sub DoWork_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim lf As New Loader
        lf.Show(Me)

        Task.Factory.StartNew(Sub()
                                  'go get the data for the grid
                                  System.Threading.Thread.Sleep(6000)
                              End Sub) _
                    .ContinueWith(Sub(result As task)
                                      'check the aggregate exception
                                      Dim aex As System.AggregateException = result.Exception
                                      If aex IsNot Nothing Then
                                          MessageBox.Show(String.Format("something bad happened: ", aex.Flatten.Message))
                                      End If

                                      'bind the data to the grid
                                      lf.Close()
                                      lf.Dispose()
                                  End Sub, TaskScheduler.FromCurrentSynchronizationContext)

    End Sub

    Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
End Class

Upvotes: 1

Related Questions