Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Using Backgroundworker to show progressbar in Form1 while Form2 loads

Posted on 2006-05-16
8
Medium Priority
?
1,078 Views
Last Modified: 2008-02-01
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?
0
Comment
Question by:ThomasBoardman
  • 4
  • 3
8 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 16696260
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16696261
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
 

Author Comment

by:ThomasBoardman
ID: 16713732
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Expert Comment

by:Madian
ID: 24809471
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24809500
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
 
LVL 1

Expert Comment

by:Madian
ID: 24814170
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24814892
"...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
 
LVL 1

Expert Comment

by:Madian
ID: 24815030
hmm i think you are right let me think about it, tanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Make the most of your online learning experience.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Six Sigma Control Plans

575 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