views:

219

answers:

1

How do I bind a ProgressBar to a property of a class updated in another thread?

The following code example shows my first naive attempt. It doesn't work because I get runtime errors about cross thread communication. I think I need to use Invoke in some way, but I'm not sure how to do it with the Binding class.

using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Threading;

class ProgressForm : Form
{
    private ProgressBar pbProgress;

    public ProgressForm(ref LongOp lo)
    {
     Binding b = new Binding("Value", lo, "Progress");
     pbProgress = new ProgressBar();
     pbProgress.DataBindings.Add(b);
     this.Controls.Add(pbProgress);
    }
}

class Program : Form
{
    private Button btnStart;
    private LongOp lo;

    public Program()
    {
     lo = new LongOp();
     btnStart = new Button();

     btnStart.Text = "Start long operation";
     btnStart.Click += new EventHandler(btnStart_Click);

     this.Controls.Add(btnStart);
    }

    private void btnStart_Click(object sender, EventArgs e)
    {
     ProgressForm pf = new ProgressForm(ref lo);
     lo.DoLongOp();
     pf.ShowDialog();
    }

    [STAThread]
    public static void Main()
    {
     Application.EnableVisualStyles();
     Application.SetCompatibleTextRenderingDefault(false);
     Application.Run(new Program());
    }
}

class LongOp : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private int progress;

    public void DoLongOp()
    {
     Thread thread = new Thread(new ThreadStart(this.run));
     thread.Start();
    }

    public void run()
    {
     for (int i = 0; i < 10; ++i)
     {
      Thread.Sleep(1000);
      Progress++;
     }
    }

    public int Progress
    {
     get
     {
      return progress;
     }

     set
     {
      progress = value;
      NotifyPropertyChanged("Progress");
     }
    }

    private void NotifyPropertyChanged(String field)
    {
     if (PropertyChanged != null)
     {
      PropertyChanged(this, new PropertyChangedEventArgs(field));
     }
    }
}

So how do I bind a ProgressBar to a value updated in another thread?

Thanks in advance

EDIT: I've switched to using the ThreadedBinding implementation Mr. Gravell wrote and linked to. I'm still getting the cross thread exception though. Pressing "Break" in the exception dialog highlights the PropertyChanged(this, new PropertyChangedEventArgs(field)); line as the line causing the exception.

What more do I need to change?

EDIT: Looks like Mr. Gravell's post has been removed. The ThreadedBinding implementation I mentioned can be found at the end of this thread: http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_thread/thread/69d671cd57a2c7ab/2f078656d6f1ee1f?pli=1

I've switched back to plain old Binding in the example for easier compilation by others.

+1  A: 

Unfortunately I think the cross-threading issues will make data-binding proper a bit too clumsy to use here, and probably more complexity than you need in any case -- the data only needs to be plumbed one way.

You could just replace the binding with an event handler like this:

private void ProgressPropertyChangedHandler(object sender,
                                            PropertyChangedEventArgs args)
{
    // fetch property on event handler thread, stash copy in lambda closure
    var progress = LongOp.Progress;

    // now update the UI
    pbProgress.Invoke(new Action(() => pbProgress.Value = progress));
}
Jeffrey Hantin
Thank you. Works great :)
Tobbe