Using Backgroundworker to show progressbar in Form1 while Form2 loads

I am trying to display a progressbar in Form1 while Form2 loads using VB 2005.  Once Form2 loads, I want to  hide Form1.  The most important aspect of this task is that I do not want to have a counter or timer controlling the progress of the progressbar.  Since Form2 could take between 30-90 seconds to load, I want to update the progressbar from the Load event for Form2.  I have struggled using delegate and invoke to update the progressbar with no success.  I have found a similar example on this site, but the example uses a counter to update the progress bar.  Any suggestions?
ThomasBoardmanAsked:
Who is Participating?
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Public Class Form1

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Form2.Show()
    End Sub

End Class


Public Class Form2

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Form1.ProgressBar1.Maximum = 5
        Form1.ProgressBar1.Value = 0
        Application.DoEvents()

        ' simulated task1
        System.Threading.Thread.Sleep(500)
        Form1.ProgressBar1.Increment(1)
        Application.DoEvents()

        ' simulated task2
        System.Threading.Thread.Sleep(500)
        Form1.ProgressBar1.Increment(1)
        Application.DoEvents()

        ' simulated task3
        System.Threading.Thread.Sleep(500)
        Form1.ProgressBar1.Increment(1)
        Application.DoEvents()

        ' simulated task4
        System.Threading.Thread.Sleep(500)
        Form1.ProgressBar1.Increment(1)
        Application.DoEvents()

        ' simulated task5
        System.Threading.Thread.Sleep(500)
        Form1.ProgressBar1.Increment(1)
        Application.DoEvents()

        Form1.Hide()
    End Sub

End Class
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Do you really want to hide Form1?   Or do you want Form1 to completely close and have Form2 now be the main form of the application?
0
 
ThomasBoardmanAuthor Commented:
Once again, Idle Mind, your suggestion did the trick!  I want to hide Form1 since it takes awhile to load, and it contains the main menu for my ap.

Tom
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
MadianCommented:
Hello i have one problem(and who not have one):) well, is it pretty similar my question

I have a form called Form1, ok?, well this form calls a procedure(named LoadData())from where i get some data from a database.

What I want are : when this procedure is called, a new instance of a form, called form2 that contains a simple progressbar with marquee style is displayed(do not report progress). and when the procedure ends the form2 will close.

While LoadData() procedure is running the original Form1 must to be locked or hidden to avoid errors or multiple calls of LoadData() procedure.

I don't want to use cancel or report progress events of a BackGroundWorker. or IsBusy property.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Ok...I don't see a "problem" there...

Just do it just like you've outlined:

    ' ...within Form1...
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Enabled = False

        Dim f2 As New Form2()
        f2.Show()

        LoadData() ' <-- this loads your data by starting the BackgroundWorker

        f2.Close()
        Me.Enabled = True
    End Sub
0
 
MadianCommented:
hmmm, tanks but, if you try to do this, your app will look frozen if don't use a thread o backgroundworker or doevents, I been trying using something like in the solution posted, but my app continue looking frozen
i use this:

//The main form from i launch this code
This.Hide();
//The progress form
Form2.Show();

//Consuming time operation
DataTable tbl = Microsoft.SqlServer.Management.Smo.SmoApplication.EnumAvailableSqlServers();

Application.DoEvents();
Form2.Close();
This.Show();

I to know the use of DoEvents() it supouse this sentence is used into a loop , but here in this particular case, all has been reduced to a single line, any sugestion?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"...your app will look frozen if don't use a thread o backgroundworker or doevents..."

Well...yeah, and I thought you already knew that from YOUR statement:

    "I don't want to use cancel or report progress events of a BackGroundWorker."

This IMPLIED that you were in fact using the BackgroundWorker() control...which would alleviate the "frozen" problem.
0
 
MadianCommented:
hmm i think you are right let me think about it, tanks
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.