Update a Progress Bar asynchronously

G

Guest

Hello

I am trying to update a Progress bar on a form. I am able to update it via using a simple clock timer, but as soon as I perform a long operation G1 (generation of a report) in a separate form, it stops updating it, but keeps on incrementing the Progress bar values in the background and displays the updated Pbar after finishing the operation G1.

My requirement is that it should update during the execution of operation G1.

On further exploration I have come to know that if I put updation of Pbar on a separate thread and use some queuing and dequeing, it would work. I have done that but behaviour remains the same. Can you Please point out where I am going wrong. I am new to Threading and Queuing.

I am attaching a part of my code. I have seen the following thread and queue are working as I have traced the flow. I may be wrong in refreshing the PBar as I have tried PBar.refresh, PBar.update after updating PBar.Value.

Dim sq as New Queu

CTimer_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs)
Tr
sField = Format(Now, "ss"
If sField = "00" The
PBar0.Value =
Els
PBar0.Value = CInt(sField
End I
PBar0.Increment(1
sq.Enqueue(PBar0.Value
End Try

Private Sub b1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b1.Clic
Dim sT As New System.Threading.Thread(AddressOf UpdateBar
sT.Start(
End Su

Private Sub UpdateBar(
Tr
D
System.Threading.Thread.CurrentThread.Sleep(2500
If sq.Count > 0 The
PBar0.Value = sq.Dequeu
'PBar0.Update(
'PBar0.Refresh(
End I
Loo
End Tr
End Sub
 
S

Scott

Why not try Application.DoEvents in the code for the long operation for
something this simple? Here's the documentation on it from the .NET
Framework SDK...

When you run a Windows Form, it creates the new form, which then waits for
events to handle. Each time the form handles an event, it processes all the
code associated with that event. All other events wait in the queue. While
your code handles the event, your application does not respond. For example,
the window does not repaint if another window is dragged on top.

If you call DoEvents in your code, your application can handle the other
events. For example, if you have a form that adds data to a ListBox and add
DoEvents to your code, your form repaints when another window is dragged
over it. If you remove DoEvents from your code, your form will not repaint
until the click event handler of the button is finished executing.

Typically, you use this method in a loop to process messages.

CAUTION Calling this method can cause code to be re-entered if a message
raises an event.


Vinay said:
Hello,

I am trying to update a Progress bar on a form. I am able to update it via
using a simple clock timer, but as soon as I perform a long operation G1
(generation of a report) in a separate form, it stops updating it, but keeps
on incrementing the Progress bar values in the background and displays the
updated Pbar after finishing the operation G1.
My requirement is that it should update during the execution of operation G1.

On further exploration I have come to know that if I put updation of Pbar
on a separate thread and use some queuing and dequeing, it would work. I
have done that but behaviour remains the same. Can you Please point out
where I am going wrong. I am new to Threading and Queuing.
I am attaching a part of my code. I have seen the following thread and
queue are working as I have traced the flow. I may be wrong in refreshing
the PBar as I have tried PBar.refresh, PBar.update after updating
PBar.Value.
Dim sq as New Queue

CTimer_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs)
Try
sField = Format(Now, "ss")
If sField = "00" Then
PBar0.Value = 1
Else
PBar0.Value = CInt(sField)
End If
PBar0.Increment(1)
sq.Enqueue(PBar0.Value)
End Try

Private Sub b1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles b1.Click
 
G

Guest

Scott

Thanks for your reply. Correct me, if I am wrong, but my understanding of Application.DoEvents - one loses the control on the ongoing event execution and as the event finishes, one will not know when it is completed unless someone verifies the end product (Report in this case).

I am not using Application.DoEvents as my report generation program is creating some interim Temp files and losing the control would allow other events to get hold of these common temp files. Also use of progress bar is valid only during the period of report generation. With Application.Doevents I would not know when the report has been generated, so that I can destroy (or hide) Progress bar control.

Besides it may look simple to you but it is not looking simple to me. It was simple for me in VB6, but in VB.net I am struggling with the required functionality. I would appreciate if someone can point to me what is wrong in my current approach as given in the post.

Regard

Vinay
 
J

John Eikanger [MSFT]

Hi, Vinay

Rather than creating a separate thread for your progress bar, it might make
more sense to put your report in a separate thread. Reporting is typically
a background task, so there are no display updating issues to address.

Thank you for choosing the MSDN Managed Newsgroups,

John Eikanger
Microsoft Developer Support

This posting is provided “AS IS” with no warranties, and confers no rights.
--------------------
| From: "=?Utf-8?B?VmluYXk=?=" <[email protected]>
| Subject: Re: Update a Progress Bar asynchronously
| Date: Thu, 26 Feb 2004 21:56:06 -0800
| X-Tomcat-NG: microsoft.public.dotnet.languages.vb
|
| Scott,

Thanks for your reply. Correct me, if I am wrong, but my understanding of
Application.DoEvents - one loses the control on the ongoing event execution
and as the event finishes, one will not know when it is completed unless
someone verifies the end product (Report in this case).

I am not using Application.DoEvents as my report generation program is
creating some interim Temp files and losing the control would allow other
events to get hold of these common temp files. Also use of progress bar is
valid only during the period of report generation. With
Application.Doevents I would not know when the report has been generated,
so that I can destroy (or hide) Progress bar control.

Besides it may look simple to you but it is not looking simple to me. It
was simple for me in VB6, but in VB.net I am struggling with the required
functionality. I would appreciate if someone can point to me what is wrong
in my current approach as given in the post.

Regards

Vinay
|
 

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