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

  • Home
  • SEARCH
  • 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 6915853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:31:38+00:00 2026-05-27T09:31:38+00:00

I know this question has been asked several times an I spent all day

  • 0

I know this question has been asked several times an I spent all day trying to understand other answers, but since I am very new to C# and WPF nothing helped me so far. I will try to explain my exact problem as much as I can so it will directly help me.

In my MainWindow.xaml I have a progress bar and some button starting a new thread and a long calculation:

<ProgressBar Height="....... Name="progressBar1"/>
<Button Content="Button" Name="button1" Click="button1_Click" />

Now within my MainWindow.xaml.cs:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        Thread thread = new Thread(new ParameterizedThreadStart(MyLongCalculation));

        ParameterClass myParameters = new ParameterClass();
        thread.Start(myParameters);
    }

    public void MyLongCalculations(object myvalues)
    {
        ParameterClass values = (ParameterClass)myvalues;
        //some calculations
    }
}

public class ParameterClass
{
    //public variables...
}

Now somehow I have to include somethign in my method MyLongCalculations that will keep updating progressBar1. However, I just can’t manage to get it working.
I know all this is very simple, but unfortunately it is the level I am at the moment on with C# so I hope an answer not too complicated and as detailed as possible would be great.

  • 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. Editorial Team
    Editorial Team
    2026-05-27T09:31:39+00:00Added an answer on May 27, 2026 at 9:31 am

    Background worker is well suited for this.

    try this:

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            // Initialize UI
            InitializeComponent();
    
            // Process data
            ProcessDataAsync(new ParameterClass { Value = 20 });
        }
    
        /// <summary>
        /// Processes data asynchronously
        /// </summary>
        /// <param name="myClass"></param>
        private void ProcessDataAsync(ParameterClass myClass)
        {
            // Background worker
            var myWorker = new BackgroundWorker
            {
                WorkerReportsProgress = true,
            };
    
            // Do Work
            myWorker.DoWork += delegate(object sender, DoWorkEventArgs e)
            {
                // Set result
                e.Result = MyLongCalculations(myClass);
    
                // Update progress (50 is just an example percent value out of 100)
                myWorker.ReportProgress(50);
            };
    
            // Progress Changed
            myWorker.ProgressChanged += delegate(object sender, ProgressChangedEventArgs e)
            {
                myProgressBar.Value = e.ProgressPercentage;
            };
    
            // Work has been completed
            myWorker.RunWorkerCompleted += delegate(object sender, RunWorkerCompletedEventArgs e)
            {
                // Work completed, you are back in the UI thread.
                TextBox1.Text = (int) e.Result;
            };
    
            // Run Worker
            myWorker.RunWorkerAsync();
        }
    
        /// <summary>
        /// Performs calculations
        /// </summary>
        /// <param name="myvalues"></param>
        /// <returns></returns>
        public int MyLongCalculations(ParameterClass myvalues)
        {
            //some calculations
            return (myvalues.Value*2);
        }
    
    }
    
    /// <summary>
    /// Custom class
    /// </summary>
    public class ParameterClass
    {
        public int Value { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that this question has been asked several times and I've read all
I know this question has been asked several times but the answers given don't
I know this question has been asked several times, but I can't quite seem
Ok, so I know this question has been asked in different forms several times,
I know this question has been asked several times and I read those with
This is a question that I know has been asked here and several other
I know this question has been asked and answered many times. But almost all
I know this question has been asked several times on SO, but none of
First, I know this question has been asked several times before and that in
I know this question has been asked several times for sure, but I have

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.