0

Hello I have a function which takes time to load. that's why i'm planning to put a progress bar on my winform so that the user would know that my program is still running. however, I do not know how I'm gonna solve it. is there someone here who could help guide me.

Here's what I'm planning to do:

 private void btnProcess_Click(object sender, EventArgs e)
        {
          //function which takes time because it contacts to a server
        }

I want to have a progressbar which increments and ends after my process has finished. Should I use a backgroundworker for this?

***I've followed this tutorial http://www.codeproject.com/Tips/83317/BackgroundWorker-and-ProgressBar-demo but it does not wait for a specific function or event to finish like a loading screen.

***My progressbar does not end after my buttonclick event has finished executing all its functions.

I've created:

private void myBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 0; i <= 100; i++)
            {
                myBackgroundWorker.ReportProgress(i);
                System.Threading.Thread.Sleep(100);
            }
        }

private void myBackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            myProgressBar.Value = e.ProgressPercentage;
        }

 private void btnProcess_Click(object sender, EventArgs e)
            {
              myBackgroundWorker.RunWorkerAsync();
              //function which takes time because it contacts to a server
            }

How Would I know when will my buttonclick event gonna end? so that my progress bar will end also?

rj tubera
  • 747
  • 4
  • 29
  • 51
  • "How Would I know when will my buttonclick event gonna end? so that my progress bar will end also?" Wire up the **RunWorkerCompleted()** event of your BackgroundWorker() control as it will fire when the DoWork() method has finished. From that event it will be safe to update controls such as resetting buttons and progressbars... – Idle_Mind May 02 '13 at 03:16

2 Answers2

0

Yeah, you should. It's pretty straightforward.

Make a background worker that executes whatever work btnProcess_Click does.

Have it report progress:

worker.WorkerReportsProgress = true;

And now you can have this progress report be triggered by an event you subscribe to.

worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);

In doing so, you can create a progress bar than can update itself based on this worker_ProgressChanged event, triggered by your computation.

You can find plenty of implementations of this just by Googling. Good luck, hope this helps.

tnw
  • 13,521
  • 15
  • 70
  • 111
  • Sir can you show me an example? Or any examples about this problem. Because I'm really confused. T_T – rj tubera May 02 '13 at 02:30
  • I said to just Google it. Did you look at any of the results? [Here's one](http://www.codeproject.com/Tips/83317/BackgroundWorker-and-ProgressBar-demo). What are you confused about? – tnw May 02 '13 at 12:28
0

These are the two really good examples

http://www.dreamincode.net/forums/topic/112547-using-the-backgroundworker-in-c%23/

http://www.dreamincode.net/forums/topic/246911-c%23-multi-threading-in-a-gui-environment/

Hope that helps

EDIT:

public partial class Form1 : Form
{
    //--------------------------------------------------------------------------
    public Form1()
    {
        InitializeComponent();

        //Initialize backgroundworker
        Shown += new EventHandler(Form1_Shown);
        backgroundWorker1.WorkerReportsProgress = true;
        backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
        backgroundWorker1.ProgressChanged +=
        new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);

        //counter
        YourObject.Counter = 100;
    }

    //--------------------------------------------------------------------------
    void btnClick_Clicked(object sender, EventArgs e)
    {
        //Trigger the background process
        backgroundWorker1.RunWorkerAsync();
    }

    //--------------------------------------------------------------------------
    void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        //Your freakishly long process,
        //which needs to be run in the background

        //to make it simple, I'll just do a for loop
        for (int i  = 0; i < 100; i++)
        {
            //The counter will keep track of your process
            //In your case, it might not be a for loop
            //So you will have to decide how to keep track of this counter
            //My suggestion is to decrease/increase this counter
            //right after importants action of your process
            backgroundWorker1.ReportProgress(YourObject.Counter--);
        }
    }

    void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        //The counter is updated and displayed with a progress bar 
        YourObject.Counter = e.ProgressPercentage;
    }
}
Thang Do
  • 316
  • 2
  • 16
  • sir, is it possible to know when will my button event finished all of its processes so that I will know when to end my progressbar? – rj tubera May 02 '13 at 02:41
  • To do so, add a counter to your progress. something like `int counter; counter++` to you progress. And use that counter to to feed to the `worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);` – Thang Do May 02 '13 at 02:46
  • sir can you explain it more sir. Because I'm new to this and I can't really follow what you are saying. Can you make it more noob-friendly? – rj tubera May 02 '13 at 02:50
  • sir what do you mean by YourObject? – rj tubera May 02 '13 at 03:10
  • sir my process should be inside a button click event. – rj tubera May 02 '13 at 03:11
  • don't mind that, you get the idea of a counter. just add that counter to your progress code. I put YourObject there just to indicate that the counter belongs to your object class. – Thang Do May 02 '13 at 03:15
  • move `backgroundWorker1.RunWorkerAsync();` to where you trigger your progress. and don't call me sir. it sounds too formal and too old for me – Thang Do May 02 '13 at 03:17
  • Sir I'm still confused in the loop part. T_T – rj tubera May 02 '13 at 03:48
  • Sir what I want to run in the background is my progressbar and not my freakishly long processs – rj tubera May 02 '13 at 03:49
  • Have you read the tutorial articles that I included above? the loop part represents your progress, which you want to run behind the scene. the progress bar is just a tool to indicate that a progress is running behind the scene. – Thang Do May 02 '13 at 20:13