Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 170579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:49:59+00:00 2026-05-11T12:49:59+00:00

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

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T12:50:00+00:00Added an answer on May 11, 2026 at 12:50 pm

    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)); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to bind the following shortcut: Ctrl + W to close tabs How
I have a progressbar in WPF C# and I bind the text of a
I'd like to bind a configuration file to my executable. I'd like to do
I'm trying to bind a list of custom objects to a WPF Image like
After a bind a method to an event of a Tkinter element is there
If I want to bind a collection to a some form of listing control
I'm trying to bind controls in a WPF form to an interface and I
When I try to bind port 80 to a socket in c, i always
I am trying to bind an event to a method of a particular instance
$(#dvMyDIV).bind(resize, function(){ alert(Resized); }); or $(#dvMyDIV).resize(function(){ alert(Resized); }); The questions Why is this not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.