Progress bar not updating while form loses focus

M

Manikandan

Hi,
I have a form with progress bar.
I'm querying sql server with large number of records(around 1 lakh)
for export
The records are written to a text file
I'm showing the process running by updating the progress bar
If the form loses focus(i.e selecting any other application) means
progress bar is not updating

The coding is below
sProgressBar.Visible = true;
sProgressBar.Minimum = 1;
sProgressBar.Maximum = NoOfRows
sProgressBar.Value = 1;
sProgressBar.Step = 1;
For loop until the rec count
{

sProgressBar.PerformStep();
}

everything work's fine until has focus, if i open some other
application and form loses focus means progress bar is not showing
progress(not updated)
I have tried form.refresh(0 in activate method, it is also not working
Is there any work around available for this problem

Thank You,
Regards,
Mani
 
M

Marc Gravell

I'm quite surrprised you see anything at all... the fundamental
problem here is doing real work on the UI thread. Really you need a
work thread and a UI thread. Fortunately, BackgroundWorker is intended
precisely for this purpose, and includes events to update progress %
(hint: you can't directly update the UI from a worker thread -
BackgroundWorker helps you do this easily).

I would suggest searching for BackgroundWorker examples; as a cowboy
alternative, Application.DoEvents() will technically do the job, but
pretty please: don't use it...

Marc
 

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