操纵形式I具有主窗口(Form1中)和一类称为模块从一个BackgroundWorker
在模块有创建一个新的BackgroundWorker和在主窗口更改标签的方法。我曾尝试在MainWindow代码中创建一个公共或内部方法,并在Module类中调用它,但它似乎不起作用。
任何人都可以帮我解决这个问题,这只是阻止我继续发展的东西。
对不起,如果我没有说清楚,如果你需要清理的东西让我知道。
Module.cs
public class Module
{
protected System.Diagnostics.PerformanceCounter cpuCounter;
BackgroundWorker cpuUThread;
private delegate void UIDelegate();
MainWindow mn;
public void runCPUUsage()
{
cpuUThread = new BackgroundWorker();
cpuUThread.DoWork += new DoWorkEventHandler(cpuUThread_DoWork);
cpuUThread.WorkerSupportsCancellation = true;
cpuUThread.RunWorkerAsync();
mn = new MainWindow();
}
void cpuUThread_DoWork(object sender, DoWorkEventArgs e)
{
cpuCounter = new System.Diagnostics.PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";
try
{
mn.changeCPUULabel(getCurrentCpuUsage().ToString());
}
catch (Exception ex)
{
}
}
public double getCurrentCpuUsage()
{
return Math.Round(cpuCounter.NextValue(), 0);
}
public void disposeCpuUsage()
{
cpuUThread.CancelAsync();
cpuUThread.Dispose();
}
}
主窗口 - 包含一个标签(labelCPUU)
internal void changeCPUULabel(string val)
{
Dispatcher.Invoke(new UIDelegate(delegate
{
this.labelCPUU.Content = val;
}));
}
public double getCurrentCpuUsage()
{
return mod.getCurrentCpuUsage();
}
void activateCPUU(){ mod.runCPUUsage(); }
从主窗口或模块的一些cample代码可能会有所帮助。 – 2010-09-03 18:05:58
请说明“似乎不起作用”。编译错误?运行时错误?意外的行为? – Bob 2010-09-03 18:07:20
什么不工作?它抛出错误还是你没有看到改变?而像史蒂夫说,某些源将是不错 – 2010-09-03 18:07:44