locked
How do I add a ProgressBar to a non UI app?

    Question

  • Hello!

     

    I have a small utility app the has no UI and is used to launch other apps, delete files or launch app installs. It ends once all tasks are complete. The number of tasks can vary for every launch of this utility and it runs through it's tasks synchronously, then ends. Sometimes completing the tasks can take several seconds or more and currently there is no indication on screen that work is being done. That is my problem.

     

    I would like to display a progress bar, during the time it takes this utility to perform it's tasks, that will show the user there is some activity going on. It does not need to show actual progress from beginning to end, because, for our purposes, it would be difficult to determine the amount of time that will be needed to complete the tasks. The important part here is to show the user there is "stuff" going on, so be patient.

     

    I am not a "noob" programmer, but I am fairly new to C#, and not very strong on how to get the various objects to work together. My app code right now is pretty simple and I understand how it works. I have also come up with (downloaded a sample) a progress bar class. What I am having a problem with is trying to figure out how to display the progressbar, showing the bar moving left to right over and over, until all tasks are finished. It seems like it should be simple, but apparently not simple enough.

     

    I have been searching the internet for examples I can use to teach me how to modify my utility app to do what I want to do, but I have not been successful thus far. I've found lots of examples of applications that have a progressbar on the form or pops up a progressbar, but they are all based on displaying a form with buttons to start the tasks, etc. This utility is currently designed to take some arguments when it is launched and use those args to perform it's tasks, then exit - all in the background, more or less, with no UI. I simply want to throw up a progressbar that shows the bar going across so the users won't wonder why nothing is happening.

     

    Does anyone have any advice on how I can, as simply as possible, add a progressbar to my small non-UI application? It was created C#  with .NET 2005. Any help would be greatly appreciated. In the meantime, I will continue my search....

     

    Thank you.

    JustRic

    justric.(NOSPAM)@gmail.com

     

     

    Friday, October 17, 2008 5:00 PM

Answers

  • The easiest way would be to add a new Form project to your exising solution, and then add a reference to the new project in your existing code.

    On this form, add a progressbar and a label.  In this project's Properties, under the Application tab, set the Output type to "Class Library" (then this progress bar can be added to other applications, by referencing just the .dll).

    Here's some simple code for an updatable progressbar:

    Code Snippet

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;


    namespace progbar

    {

    public partial class ProgressBarForm : Form

    {

    InitializeComponent();

    // set these properties to what you like.

    this.StartPosition = FormStartPosition.CenterScreen;

    this.FormBorderStyle = FormBorderStyle.None;

    this.Width = 380;

    this.Height = 65;

    this.progressBar1.Maximum = 100;


    this.Show();

    }


    public void UpdateProgressInfo(string message)

    {

    if(this.progressBar1.Value == 100)

    this.progressBar1.Value = 0; // reset progress bar


    this.progressBar1.Value++;


    this.label1.Text = message;


    this.Refresh();

    }


    public void KillProgBar()

    {

    this.Close();

    }

    }


    An example of how to use this:

    Code Snippet

    class Program

    {

    static void Main(string[] args)

    {

    // example of starting the progbar:

    ProgressBarForm pbf = new ProgressBarForm();

    // example of updating:

    for (int i = 0; i < 15000; i++)

    {

    pbf.UpdateProgressInfo("I am on number: " + i.ToString());

    }


    // close the progressbar

    pbf.KillProgBar();

    }

    }



    By putting the .Show() and .Close() methods in the ProgressBarForm class, we have negated the need to include System.Windows.Forms in your original application.  The only thing needed is a reference to the ProgressBarForm, and then create, update and close at will.  





    Saturday, October 18, 2008 2:12 PM

All replies


  • This forum is for C++ issues. I suggest you post this in
    a C# forum.

     

    - Wayne

    Friday, October 17, 2008 6:03 PM
  • um, this is a C# forum.

    and i don't think its possible to add a progressbar to a nonui app becuase progress is part of the ui.

    u need to make it a ui app to add a progressbar

     

    Saturday, October 18, 2008 1:57 PM
  • The easiest way would be to add a new Form project to your exising solution, and then add a reference to the new project in your existing code.

    On this form, add a progressbar and a label.  In this project's Properties, under the Application tab, set the Output type to "Class Library" (then this progress bar can be added to other applications, by referencing just the .dll).

    Here's some simple code for an updatable progressbar:

    Code Snippet

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;


    namespace progbar

    {

    public partial class ProgressBarForm : Form

    {

    InitializeComponent();

    // set these properties to what you like.

    this.StartPosition = FormStartPosition.CenterScreen;

    this.FormBorderStyle = FormBorderStyle.None;

    this.Width = 380;

    this.Height = 65;

    this.progressBar1.Maximum = 100;


    this.Show();

    }


    public void UpdateProgressInfo(string message)

    {

    if(this.progressBar1.Value == 100)

    this.progressBar1.Value = 0; // reset progress bar


    this.progressBar1.Value++;


    this.label1.Text = message;


    this.Refresh();

    }


    public void KillProgBar()

    {

    this.Close();

    }

    }


    An example of how to use this:

    Code Snippet

    class Program

    {

    static void Main(string[] args)

    {

    // example of starting the progbar:

    ProgressBarForm pbf = new ProgressBarForm();

    // example of updating:

    for (int i = 0; i < 15000; i++)

    {

    pbf.UpdateProgressInfo("I am on number: " + i.ToString());

    }


    // close the progressbar

    pbf.KillProgBar();

    }

    }



    By putting the .Show() and .Close() methods in the ProgressBarForm class, we have negated the need to include System.Windows.Forms in your original application.  The only thing needed is a reference to the ProgressBarForm, and then create, update and close at will.  





    Saturday, October 18, 2008 2:12 PM

  • Amrykid said:
    Quote>um, this is a C# forum.

     

    The message was originally posted in the Visual C++
    Express Edition forum. When one of the moderators
    moved it from there to here my reply went with it.

     

    - Wayne

    Saturday, October 18, 2008 3:09 PM