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

Ask A Question

Stats

  • Questions 124k
  • Answers 124k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are you running on OS 3.0? I saw the same… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer It looks like you need to register Apache::Session::Memcached with Apache::Session::Wrapper,… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer Use DATENAME or DATEPART: SELECT DATENAME(dw,GETDATE()) -- Friday SELECT DATEPART(dw,GETDATE())… May 12, 2026 at 1:19 am

Related Questions

how do I bind a std::ostream to either std::cout or to an std::ofstream object,
How do i bind a regular asp.net dropdownlist to an enum?
In my .emacs i have the following function that transposes a line (defun move-line
I have a datasource that I want to bind to a listview that has

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.