PC Review


Reply
Thread Tools Rate Thread

Show progress in a Windows Forms application

 
 
Marinos Christoforou
Guest
Posts: n/a
 
      10th Jan 2005
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.


 
Reply With Quote
 
 
 
 
Carlos J. Quintero [.NET MVP]
Guest
Posts: n/a
 
      10th Jan 2005
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


"Marinos Christoforou" <(E-Mail Removed)> escribió en el mensaje
news:(E-Mail Removed)...
>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.
>



 
Reply With Quote
 
 
 
 
Bob Powell [MVP]
Guest
Posts: n/a
 
      10th Jan 2005
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.





"Marinos Christoforou" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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.
>



 
Reply With Quote
 
Carlos J. Quintero [.NET MVP]
Guest
Posts: n/a
 
      10th Jan 2005
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


"Marinos Christoforou" <(E-Mail Removed)> escribió en el mensaje
news:(E-Mail Removed)...
>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.
>



 
Reply With Quote
 
William Stacey [MVP]
Guest
Posts: n/a
 
      10th Jan 2005
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.

--
William Stacey, MVP
http://mvp.support.microsoft.com

"Marinos Christoforou" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>
>


 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      10th Jan 2005
"Marinos Christoforou" <(E-Mail Removed)> schrieb:
>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 S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
 
Reply With Quote
 
Marinos Christoforou
Guest
Posts: n/a
 
      11th Jan 2005
Hi Carlos,

I am actually doing it synchronously at this current stage.

"Carlos J. Quintero [.NET MVP]" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> 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
>
>
> "Marinos Christoforou" <(E-Mail Removed)> escribió en el mensaje
> news:(E-Mail Removed)...
>>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.
>>

>
>



 
Reply With Quote
 
Marinos Christoforou
Guest
Posts: n/a
 
      11th Jan 2005
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.

"Marinos Christoforou" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
NEED Help - How to capture the Event's progress in Progress Bar =?Utf-8?B?U3JpcmFt?= Microsoft Excel Programming 6 22nd Aug 2006 12:04 PM
Would like to have a smooth progress bar or a custom image in the progress bar jayderk Microsoft C# .NET 2 4th May 2005 06:05 AM
Progress Bar progress Dino Buljubasic Microsoft VB .NET 2 10th Mar 2005 02:17 AM
How to display the Progress (%) in the middle of a Progress Bar... =?Utf-8?B?a2Fqb2w=?= Microsoft Dot NET Framework Forms 4 28th Sep 2004 02:15 PM
Progress bar indicating query progress =?Utf-8?B?cGF1bA==?= Microsoft Access VBA Modules 1 23rd Feb 2004 05:08 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:55 PM.