Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating a progress bar in a C# GUI from another thread and class
    text
    copied!<blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/924108/updating-a-progress-bar-from-another-thread">Updating a Progress Bar from Another Thread</a> </p> </blockquote> <p>In my program, I wanted to separate non-GUI functions to another class, and leave things related to the GUI in the main class. However, I am having issues with updating a progress bar while one of the worker methods in the worker class is doing its job. I know that I will have to work with multithreading here, but I do not understand how. I may just be missing simple things, but when I look for information about it, it seems that most tutorials talk about the minutia, but do not explain the big picture very well. I partially understand what invoke and delegate commands are, but I don't really understand how they interact.</p> <p>Below is stripped down version of what I want to do. How do I modify this to update the progress bar, but keep the window responsive and repainting?</p> <p>Main form class:</p> <pre><code>public partial class Form1 : Form { time_waster thing = new time_waster(); public Form1() { InitializeComponent(); progressBar1.Minimum = 0; progressBar1.Maximum = 100; } private void button1_Click(object sender, EventArgs e) { thing.something_that_takes_a_while(); } } </code></pre> <p>Separate worker class: class time_waster { public time_waster() { }</p> <pre><code> public void something_that_takes_a_while() { int delay = 200; for (int i = 0; i &lt; 100; i++) { Thread.Sleep(delay); //appropriate code to update the progress bar for each iteration of the for loop. } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload