How to update the progress bar in a modal window?

M

Mae

Hi,

I'm a novice in .Net C#. I wrote a code to show the progress of exporting file from datagridview. The progress bar will be shown in a pop-up window.

The code is shown as below:

BackgroundWorker bgw = new BackgroundWorker();

bgw.DoWork += delegate
{
//code to export data from datagridview
};

bgw.RunWorkerAsync();
FrmProgress progress = new FrmProgress();
progress.Show();

bgw.RunWorkerCompleted += delegate
{
progress.Close();
};

The progress window is shown while exporting is running on another thread, but I do not know how to update the progressbar in the modal window, so that it show the percentage of file being written.

Can anyone help me with this?

Thanks in advance.
 
I

Ignacio Machin ( .NET/ C# MVP )

Hi,

I'm a novice in .Net C#. I wrote a code to show the progress of exporting file from datagridview. The progress bar will be shown in a pop-up window.

The code is shown as below:

BackgroundWorker bgw = new BackgroundWorker();

bgw.DoWork += delegate
{
//code to export data from datagridview
};

bgw.RunWorkerAsync();
FrmProgress progress = new FrmProgress();
progress.Show();

bgw.RunWorkerCompleted += delegate
{
progress.Close();
};

The progress window is shown while exporting is running on another thread, but I do not know how to update the progressbar in the modal window, so that it show the percentage of file being written.

Can anyone help me with this?

Thanks in advance.

Hi,

How do you know the progress to start with? You haven't post the
actual export code.
In short, you send a message to the modal window using
Control.Invoke , you need the reference to a control in the form (it
can be any control)
 
W

Walter Frank

Hi Mae,

you could add

public void inc() {progressBar1.Value++;}

in your FrmProgress to increment the progress bar.
You can then call this from your worker thread using

progress.Invoke(new MethodInvoker(progress.inc));

This will run progress.inc() in the Thread that created the Form progress.

If you want to set specific values on the ProgressBar (pass a parameter),
add

public void SetProgress (int Value)
{progressBar1.Value = Value;}

to your FrmProgress to set the value of the progress bar.
Then add

delegate void ProgressInvoker (int Value);

to your main class and call

progress.Invoke(new ProgressInvoker(progress.SetProgress), progressValue);

where progressValue is the desired int value.


By the way...
You should call
bgw.RunWorkerAsync();
after
bgw.RunWorkerCompleted += delegate{...}
and
progress = new FormProgress();
not before.

Hope this will help...
Walter


in message news:[email protected]...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top