2016-06-07 54 views
0

Program.cs的C#应用程序

namespace PerformanceMonitor 
{ 
    static class Program 
    { 
     private static int NumberOfCores; 
     private static List<int> CPULoadVals; 

     [STAThread] 
     static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      Application.Run(new MonitorGUI()); 

      NumberOfCores = getNumberOfCores(); 
      CPULoadVals = getCoreLoadVals(); 
     } 

     private static int getNumberOfCores() 
     { 
      int coreCount = 0; 
      foreach (var core in new ManagementObjectSearcher("SELECT * FROM Win32_Processor").Get()) 
      { 
       coreCount += int.Parse(core["NumberOfCores"].ToString()); 
      } 
      return coreCount; 
     } 
     ... 

MonitorGUI.cs

namespace PerformanceMonitor 
{ 
    public partial class MonitorGUI : Form 
    { 
     public static List<Label> labels; 
     private static List<int> CPULoadVals; 

     public MonitorGUI() 
     { 
      InitializeComponent(); 
     } 

     public void Form1_Load(object sender, EventArgs e) 
     { 
      ... 
     } 

调试应用程序,我可以看到InitializeComponent()调用导致一种新形式(Application.Run(new MonitorGUI());),但尝试之后,没有什么被调用。在窗体加载的方法并不甚至称,即使我可以直观地看到,它的加载

+2

您是否正在分配给Load事件中的任一个列表?他们是否已初始化? –

+3

你有没有像这样连接任何Load事件this.Load + = new System.EventHandler(this.Form1_Load);? – riteshmeher

+0

这两个列表都没有初始化,我没有连线任何事件 – wmash

回答

1

Application.Run()

开始运行当前线程上的标准应用程序消息循环,并使指定窗体可见。

这种方法只有当你关闭Form作为参数传递返回。所以之后的所有通话都会在您关闭主窗口时执行。

您可能要更改顺序:

[STAThread] 
static void Main() 
{ 
    NumberOfCores = getNumberOfCores(); 
    CPULoadVals = getCoreLoadVals(); 

    Application.EnableVisualStyles(); 
    Application.SetCompatibleTextRenderingDefault(false); 
    Application.Run(new MonitorGUI()); 
} 

而且Form1_Load()如果你订阅了Load事件Form的只叫:

public MonitorGUI() 
{ 
    InitializeComponent(); 
    Load += Form1_Load; // <--- subscribe to the event 
} 

但是,这也可能是设计师完成。检查您是否已正确设置此事件。

+0

非常感谢!这已经成功了。将接受我什么时候可以:) – wmash