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 6917549
In Process

The Archive Base Latest Questions

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

Learning C#, WPF. I’ve come across a problem I can’t solve by research alone.

  • 0

Learning C#, WPF. I’ve come across a problem I can’t solve by research alone.

I want to update the text of a textbox control from another thread which is present in another class.

I know the thread is started, working and has data I can use to populate the text box. What I can’t figure out is how to address the text box control in the GUI from the second thread.

My text box control is called ‘txt_CPU’ and I want ‘cpuCount’ to appear in it. I’m sure I need to use delegation, but I can’t relate the examples to my code.

Help is appreciated. (I’m sure there are may other ‘problems’ in my code, this is rough learning in progress)

So we have the thread creation.

 public MainWindow()
        {
            InitializeComponent();

            //start a new thread to obtain CPU usage
            PerformaceClass pc = new PerformaceClass();
            Thread pcThread = new Thread(pc.CPUThread); 
            pcThread.Start();

        }

The Class it’s calling

 public class PerformaceClass
    {

            public string getCPUUsage()
            {
                PerformanceCounter cpuCounter;

                cpuCounter = new PerformanceCounter();

                cpuCounter.CategoryName = "Processor";
                cpuCounter.CounterName = "% Processor Time";
                cpuCounter.InstanceName = "_Total";
                return cpuCounter.RawValue.ToString() + "%";

            }

        public void CPUThread()
        {
            PerformaceClass PC = new PerformaceClass();

            int i = 0;
            while (i < 5)
            {
                string cpuCount = PC.getCPUUsage();
                i++;
                System.Threading.Thread.Sleep(500);
                // MessageBox.Show(cpuCount);

            }   
        }
    }
  • 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:44:41+00:00Added an answer on May 27, 2026 at 9:44 am

    One way is to add an event handler to your PerformaceClass like this:

     public class PerformanceClass
     {
         public event EventHandler<PerformanceEventArgs> DataUpdate;
    
         ....
         public void CPUThread()
         {
            int i = 0;
            while (i++ < 5)
            {
                string cpuCount = getCPUUsage();
                OnDataUpdate(cpuCount);
                System.Threading.Thread.Sleep(500);
    
            }   
         }
    
         private void OnDataUpdate(string data)
         { 
              var handler = DataUpdate;
              if (handler != null)
              {
                   handler(this, new PerformanceEventArgs(data));
              }
         }
     }
    
     public class PerformanceEventArgs: EventArgs
     {
           public string Data { get; private set; }
           public PerformanceEventArgs(string data)
           {
                Data = data;
           }
     }
    

    Then use it in your main like this:

    public MainWindow()
    {
        InitializeComponent();
    
        //start a new thread to obtain CPU usage
        PerformanceClass pc = new PerformanceClass();
        pc.DataUpdate += HandleDataUpdate;
        Thread pcThread = new Thread(pc.CPUThread); 
        pcThread.Start();
    }
    
    private void HandleDataUpdate(object sender, PerformanceEventArgs e)
    {
         // dispatch the modification to the text box to the UI thread (main window dispatcher)
         Dispatcher.BeginInvoke(DispatcherPriority.Normal, () => { txt_CPU.Text = e.Data });
    }
    

    Note that I fixed the typo in the PerformanceClass (missing n).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just start learning WPF, is that possible I can create an IE toolbar
I'm just learning WPF. I'm using VS 2008. I've added an Image control to
I've been using Winforms since .NET 1.1 and I want to start learning WPF.
I'm learning WPF and can't figure out how to enfore my buttons to take
I've been learning WPF and checking out Expression Blend. As far as I can
Still picking my way through learning XAML/WPF. If someone can show me how to
I'm learning WPF. I want to provide my own Main method in my App.xaml.cs
So I am learning WPF right now, and want to do a simple databind
I am learning WPF and WCF. I created a WPF User Control Library and
We can make WPF Custom Control for overriding some control's look&feel, like icon button.

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.