Show progress in a Windows Forms application

Discussion in 'Microsoft Dot NET Framework Forms' started by Marinos Christoforou, Jan 10, 2005.

  1. 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.
     
    Marinos Christoforou, Jan 10, 2005
    #1
    1. Advertisements

  2. 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" <> escribió en el mensaje
    news:...
    >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.
    >
     
    Carlos J. Quintero [.NET MVP], Jan 10, 2005
    #2
    1. Advertisements

  3. 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" <> wrote in message
    news:...
    >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.
    >
     
    Bob Powell [MVP], Jan 10, 2005
    #3
  4. 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" <> escribió en el mensaje
    news:...
    >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.
    >
     
    Carlos J. Quintero [.NET MVP], Jan 10, 2005
    #4
  5. 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" <> wrote in message
    news:...
    > 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.
    >
    >
     
    William Stacey [MVP], Jan 10, 2005
    #5
  6. "Marinos Christoforou" <> 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/>
     
    Herfried K. Wagner [MVP], Jan 10, 2005
    #6
  7. Hi Carlos,

    I am actually doing it synchronously at this current stage.

    "Carlos J. Quintero [.NET MVP]" <> wrote in
    message news:...
    > 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" <> escribió en el mensaje
    > news:...
    >>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.
    >>

    >
    >
     
    Marinos Christoforou, Jan 11, 2005
    #7
  8. 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" <> wrote in message
    news:...
    >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.
    >
     
    Marinos Christoforou, Jan 11, 2005
    #8
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. babylon

    MessageBox.Show don't show up after Application.Run()

    babylon, Feb 16, 2004, in forum: Microsoft Dot NET Framework Forms
    Replies:
    5
    Views:
    673
    Ellery Familia
    Feb 17, 2004
  2. John Galt

    How do I show download progress?

    John Galt, May 20, 2004, in forum: Microsoft Dot NET Framework Forms
    Replies:
    3
    Views:
    404
    Bob Powell [MVP]
    May 21, 2004
  3. Guest

    How to display the Progress (%) in the middle of a Progress Bar...

    Guest, Sep 24, 2004, in forum: Microsoft Dot NET Framework Forms
    Replies:
    4
    Views:
    1,204
    Guest
    Sep 28, 2004
  4. Guest

    How to show popup progress window?

    Guest, Jan 10, 2005, in forum: Microsoft Dot NET Framework Forms
    Replies:
    4
    Views:
    927
    Guest
    Jan 11, 2005
  5. Jordan S.

    .NET Windows Forms Application VS MS Access client Application

    Jordan S., Apr 13, 2006, in forum: Microsoft Dot NET Framework Forms
    Replies:
    22
    Views:
    555
    Jordan S.
    Apr 19, 2006
Loading...

Share This Page