Update Label while processing in Windows Forms

You'll need to get your data from one thread to the other. This can be done in a couple of ways...

First, your "background" thread could update some kind of "CurrentStatus" string variable that it changes as it goes along. You could then put a timer on your form that would then grab the CurrentStatus variable and update the label with it.

Second, you could simply invoke the operation from the background thread to the UI thread with a delegate using the InvokeRequired property of the label control. So for example...

private delegate void UpdateStatusDelegate(string status);
private void UpdateStatus(string status)
{
    if (this.label1.InvokeRequired)
    {
        this.Invoke(new UpdateStatusDelegate(this.UpdateStatus), new object[] { status });
        return;
    }

    this.label1.Text = status;
}

You can call that UpdateStatus() method from any thread (UI or background), and it will detect whether or not it needs to invoke the operation on the main UI thread (and if so, does it).

To actually set up the thread, you can do so like this:

    private void StartProcessing()
    {
        System.Threading.Thread procThread = new System.Threading.Thread(this.Process);

        procThread.Start();
    }

    private void Process() // This is the actual method of the thread
    {
        foreach (System.IO.FileInfo f in dir.GetFiles("*.txt"))
        {
            // Do processing
            // Show progress bar
            // Update Label on Form, "f.Name is done processing, now processing..."
            UpdateStatus("Processing " + f.Name + "...");                
        }
    }

Then when the user clicks the "GO" button you'll simply call StartProcessing().


A quick fix for you would be:

Label1.Text = f.Name + " is done processing, now processing...";
Label1.Refresh();

You really want to avoid DoEvents, otherwise you'll have problems if your user repeatedly presses buttons on your form.


You should be doing this on another thread, and then updating your UI thread from that thread. You are blocking further processing by performing this work on the UI thread.

If you can't move this code to the UI thread, then you could always call Application.DoEvents, but I strongly suggest you explore these options first:

  • System.ComponentModel.BackgroundWorker
  • System.Threading.ThreadPool
  • System.Threading.Thread
  • System.Threading.Tasks namespace

If your processing is lengthy do it in a backgroundworker thread.

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx