Click here to Skip to main content
15,897,334 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
Hi,

I have created a C# windows application which contains comboboxes and buttons.

When a user selects a value from the combobox a query is fired on the database

which takes a noticable time to execute.

I am displaying a progressbar on the form to notify this to the user but in order

to avoid user from clicking the buttons and selecting other comboboxes till the

operation finishes I want to show a popup progressbar.

I know this can be done using showdialog but how to close the showdialog form when

the function finishes its execution.

Also if you have any other alternative,please suggest.

Thank you,

Have a good day ahead,
=====================
Kishor Kadam
Contact:[Deleted]
======================
Posted
Updated 24-Jan-13 22:29pm
v2

Surely you are updating the value of the progress bar. Once the progress bar is >= 100% just call myDialog.Close();
 
Share this answer
 
This is really easy to using a 'BackGroundWorker Component, and a 'ProgressBar Control.

The 'BackGroundWorker gives you an easy way to run an operation asynchronously.

Since you want to "freeze" the UI, you need to use 'ShowDialog, so you will need a Form to put your ProgressBar on. In that Form we need to provide a way for an external "customer" to update the ProgressBar:
C#
// expose a method to set the ProgressBar #value
public void SetProgressBarValue(int pbValue)
{
    progressBar1.Value = pbValue;
}
The BackGroundWorker exposes three main Events that you can handle:

1. DoWork: this is where you will start the Query, and this is where you must figure out how to meaningfully update the ProgressBar.

2. ProgressChanged: this Event which is, strangely, called by using the ReportProgress method of the BackGroundWorker is passed an integer value, and it is in this EventHandler you'll update the ProgressBar.

3. RunWorkerCompleted: triggered for you when the BackGroundWorker is done, and this is where you will close the Form that displays the DialogBox.

Assuming your Form with the ProgressBar is named 'ProgressBarForm, and in your main Form's code you have created an instance of it assigned to a variable named 'frmPgBar, and your BackGroundWorker Component (drag-dropped onto your main form from the ToolBox) is named 'bgwQuery

Starting the BackGroundWorker:

1. in your EventHandler for the user-interface element that now triggers the Query:
C#
bgwQuery.RunWorkerAsync();
frmPg.ShowDialog();
Now your code in your 'DoWork EventHandler is going to be executed, and you face the challenge of figuring out how to update the ProgressBar with some meaningful value so that when you call 'ReportProgress(#integer) in your 'DoWork code:
C#
private void bgwQuery_DoWork(object sender, DoWorkEventArgs e)
{    
     while (? some condition)
     {
        // do stuff

        // generate an integer value in the range 0~100
        // for the update
        bgwQuery.ReportProgress(#integer);
     }
}
In the ProgressChanged EventHandler:
C#
private void bgwQuery_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    // update the ProgressBar in frmPgBar
    frmPgBar.SetProgressBarValue(e.ProgressPercentage);
}
Finally, in your RunWorkerCompleted handler, you can close the Form with the ProgressBar:
C#
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    frmPgBar.Close();
}
Note for other advanced uses of BackGroundWorker including passing parameters into the 'DoWork EventHandler, using assignment to the 'Result field of DoWork's DoWorkEventArgs to transfer data back to the main UI thread, etc., see MSDN docs.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900