Note that there are some explanatory texts on larger screens.

plurals
  1. POC# forms: progressbar doesn't show updates from thread doing work
    primarykey
    data
    text
    <h3>Problem</h3> <p>How can I do work in one thread and update a progressbar on the main thread?</p> <h3>Summary</h3> <p>Below is my attempt to solve the problem, but it's not working. I believe the reason is that the updates are getting queued in the Windows message pump, but then all executed at once so they happen so fast the user doesn't see them.</p> <h3>Details</h3> <p>I'm trying to create a GUI app that will do work with threads in the background and update a progressbar. I realize that I would have a simpler time using a backgroundworkerprocess but that is taking away some flexibility I need. </p> <p>I'm using InvokeRequired and callback functions to handle calls from the worker thread to the main thread so thread safety shouldn't be an issue. </p> <p>On my PC when I click button1 it takes the worker thread about 5 seconds to finish its work and output the results to textbox1. During that time the worker thread makes about 100 calls to the updateProgressBar function. This function is executing: </p> <pre><code>progressBar1.Value = percent; progressBar1.Update(); progressBar1.Refresh(); progressBar1.Invalidate(); </code></pre> <p>However, the effects of these commands are never seen by the user. I believe they are all getting put into the message pump to update the GUI, but that they are just getting queued up and all executed at once. </p> <p>I've tried inserting sleeps and playing around with the flow of the work being done but that hasn't helped. How can I get the progressbar updates flushed?</p> <p>Just to note, this is a contrived example to illustrate the problem.</p> <h3>Code</h3> <pre><code>// Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; using System.Text.RegularExpressions; namespace ProgressBarExample { public partial class Form1 : Form { Thread processThread; delegate void updateGUICallback(int read, int unread); delegate void updateProgressBarCallback(int percent); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string input = "abc1234 123456 domain\abc1234 123 0\r\n"; // Create 1048576 lines of input for (int i = 0; i &lt; 20; i++) input += input; processThread = new Thread(new ParameterizedThreadStart(processData)); processThread.Start(input); } private void processData(object input) { // Break the input on newlines string[] lines = Regex.Split((string)input, "\r\n"); int readLines = 0; int unreadLines = 0; int maxLines = lines.Length; // Setup for regex Match match; string pattern = @"^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)$"; // Read each line of input for (int i = 0; i &lt; maxLines; i++) { // Attempt a regex match match = Regex.Match(lines[i].Trim(), pattern); // If the line matches our expected entry, add it to the list if (match.Success) { readLines++; } else { unreadLines++; } // Update the progressbar every 10000 iterations if (i % 10000 == 0) { updateProgressBar(i / maxLines); } } updateTextbox(readLines, unreadLines); } private void updateTextbox(int read, int unread) { if (textBox1.InvokeRequired) { updateGUICallback cb = new updateGUICallback(updateTextbox); this.Invoke(cb, new object[] { read, unread }); } else { textBox1.Text = "Read: " + read + "\r\n" + "Unread: " + unread; } } private void updateProgressBar(int percent) { if (progressBar1.InvokeRequired) { updateProgressBarCallback cb = new updateProgressBarCallback(updateProgressBar); this.Invoke(cb, new object[] { percent }); } else { progressBar1.Value = percent; progressBar1.Update(); progressBar1.Refresh(); progressBar1.Invalidate(); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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