Show progress in a Windows Forms application

  • Thread starter Marinos Christoforou
  • Start date
M

Marinos Christoforou

I have a VB.NET Windows Forms application. The main form calls a worker
class to do some work. Currently I implement this call syncronously and
would like to show the progress via a progress bar as this work is being
carried out. What is the recommended method of doing this?

One way I thought was to write a public method say "ShowProgress" in the
main form's class. However I am not sure how the worker class would call
this method given that there is no explicit instance of the main form, but
rather it is initiated as a type parameter to the Application.Run method.

Thanks in advance.
 
C

Carlos J. Quintero [.NET MVP]

You can add a ProgressChanged event to your worker class and raise it
whenever there is a significant progress change (minimum 1%). The main form
can then capture that event from the worker class and update the progress
bar. This way the worker class knows nothing about its controller (the
form).

About the second question, Application.Run receives a form instance but it
is the form who creates the worker class and therefore could pass a
reference to itself (this) to the worker class in its constructor if you
prefer the worker class changing the progress bar of the form.

--

Carlos J. Quintero

MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
You can code, design and document much faster.
http://www.mztools.com
 
B

Bob Powell [MVP]

You need to give your worker thread a delegate that it can call to signal
progress. When the handler for that delegate in the main form receives the
callback it should check to see if the form requires invoke and possibly
BeginInvoke of the method that updates the progress. Otherwise it can just
update the progress bar as normal.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
C

Carlos J. Quintero [.NET MVP]

Marinos,

Are you doing it "syncronously" (sic) or asynchronously (that is, threads
involved)? The answer is quite complicated if you meant asynchronously,
there are several articles about that...

--

Carlos J. Quintero

MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
You can code, design and document much faster.
http://www.mztools.com
 
W

William Stacey [MVP]

Something like this in your form:

private delegate void UpdateDelegate(int count);

public void UpdateBar(int count)
{
if ( InvokeRequired )
{
UpdateDelegate ud = new UpdateDelegate(UpdateBar);
BeginInvoke(ud, new object[]{count});
}
else
{
this.pBar.Value = count;
}
}

Then you can pass form ref in at worker constructor or set a public property
on your worker class so it has a ref to form. The worker can then just call
"this.form1.UpdateBar(x)" to update the progress bar. Subscribing to an
event on the worker would work too but I always forget how to string them up
off the top of my head, but I can remember this and works as a good pattern
for most things to update the GUI thread from other threads or the same
thread - so it has dual use as you can call it from your form too (i.e. a
button, etc) to leverage the logic. Can mod to update any kind of control.
 
H

Herfried K. Wagner [MVP]

Marinos Christoforou said:
I have a VB.NET Windows Forms application. The main form calls a worker
class to do some work. Currently I implement this call syncronously and
would like to show the progress via a progress bar as this work is being
carried out. What is the recommended method of doing this?

A .NET Progress Dialog
<URL:http://www.codeproject.com/cs/miscctrl/progressdialog.asp>

Related:

Multithreading in Windows Forms applications
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=multithreading&lang=en>
 
M

Marinos Christoforou

Hi Carlos,

I am actually doing it synchronously at this current stage.
 
M

Marinos Christoforou

Thanks to everyone that replied. I will carefully read through all the
answers, but it seems that you're all in agreement that the best way to do
handle such situations is through delegates and/or events.

Regards.
 

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