c# - 从另一个类中的另一个线程更新文本框内容。 C#、WPF

标签 c# wpf multithreading

学习C#、WPF。我遇到了一个仅靠研究无法解决的问题。

我想从另一个类中存在的另一个线程更新文本框控件的文本。

我知道线程已启动、正在工作并且具有可用于填充文本框的数据。我不知道如何从第二个线程处理 GUI 中的文本框控件。

我的文本框控件名为“txt_CPU”,我希望“cpuCount”出现在其中。我确信我需要使用委托(delegate),但我无法将这些示例与我的代码联系起来。

感谢帮助。 (我确信我的代码中可能还存在其他“问题”,这是正在进行的粗略学习)

所以我们创建了线程。

 public MainWindow()
        {
            InitializeComponent();

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

        }

它调用的类

 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);

            }   
        }
    }

最佳答案

一种方法是将事件处理程序添加到您的 PerformaceClass 中,如下所示:

 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;
       }
 }

然后在你的 main 中使用它,如下所示:

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 });
}

请注意,我修复了 PerformanceClass 中的拼写错误(缺少 n)。

关于c# - 从另一个类中的另一个线程更新文本框内容。 C#、WPF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8458956/

相关文章:

c# - 发送 web header 集合

对 pthread_cond_wait 的困惑

java - 让一个线程等待另一个线程完成

c# - 如何并行化 Kinect AllFramesReady 事件

c# - 如何以编程方式设置 Viewbox 的内容?

C# - 数学公式帮助 - 作业

wpf - 你如何解决这个 LostFocus/LostKeyboardFocus 问题?

wpf - 有没有一种方法可以使用WPF网格而不为每个项目指定绝对单元格坐标?

WPF TextBox重新计算大小

c# - 使用计时器将 List<class> 保存到 DB 时出现问题,并且 List 由多线程写入