Solved

BackgroundWorker

Posted on 2008-10-02
4
944 Views
Last Modified: 2013-12-17
I have a form that load data from a database.  While the form is loading I have a pop-up window that says please wait.  I am trying to use the backgroundworker to have it run while the form is loading and disappear after it is done.    How do I use the BackgroundWorker methods (DoWork, RunWorkerCompleted, ProgressChanged) to open a dialog that says "please wait while form is loading" and then automatically closes once the form has loaded.
0
Comment
Question by:MichelleLacy
  • 3
4 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 22623052
You can also use a toolstripstatusbar like this example:
http://www.c-sharpcorner.com/UploadFile/LivMic/BGWorker07032007000515AM/BGWorker.aspx
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 22623056
Use the BackgroundWorker component in .NET 2.0.

private Form pleaseWait;
 

private void buttonStart_Click(object sender, EventArgs e)

{

    pleaseWait = new FormPleaseWait();

    pleaseWait.Show();

    backgroundWorker1.RunWorkerAsync();

}
 

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)

{

    // Perform a long-running operation here...

}
 

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)

{

    pleaseWait.Close();

} 

Open in new window

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 22623064
I use the ToolStripProgressBar and a label to update the page as the long running process is running in my class.

I have a Win Form called frmActivation and the big process is in a class called cActivation.
So showing the relevant parts I have:

Source: http://discuss.joelonsoftware.com/default.asp?dotnet.12.563832.5
Public Class frmActivation

'The class that has the long running process in it.

  Private WithEvents _Activation As ADEActivationLib.AdvancedDigitalEngines.cActivation

  Private _tspb As ToolStripProgressBar
 

' The start of the big process

    Private Sub Submit()

    

...

        _tspb = CType(CType(Me.ParentForm, MDIParent1).ToolStrip1.Items("ToolStripProgressBar1"), ToolStripProgressBar)

        _tspb.Enabled = True

        _tspb.Value = 10

        Me.lblStatus.Text = "Please Wait, this may take a minute or two."

        Me.lblStatus.Visible = True

        Me.Refresh()
 

    _Activation = New ....
 
 

    End Sub
 
 
 

'An update method that gets called by cActivation at different steps in its processing. 

'Notice the "Handles" part.
 

    Private Sub StatusUpdate(ByVal Message As String) Handles _Activation.StatusChanged

        Me.lblStatus.Text = Message

        _tspb.Value = _tspb.Value + 10

        Me.Refresh()
 

    End Sub
 

End Class
 

'Then in cActivation at different steps I have the following calls:
 

RaiseEvent StatusChanged("Setting up Activation")

RaiseEvent StatusChanged("Getting License from WebService")

RaiseEvent StatusChanged("Geting Current keys if any")

RaiseEvent StatusChanged("Confirming License")

RaiseEvent StatusChanged("Evaluating / Saving License")

etc.

Open in new window

0
 
LVL 5

Expert Comment

by:jose_juan
ID: 22623082
Hi,

set WorkerReportsProgress to true.
define the RunWorkerCompleted to a event handler, when this will be fired then close your window alert.

Good luck!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
which object should i use (efficiency) 5 45
C# Offline Apllication 5 48
Remove greater than sign 3 40
Need a starter for ETL protocol? 4 31
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

948 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now