0

Program.cs

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)
        {
            ...
        }

Debugging the app I can see that InitializeComponent() is invoked causing a new form to be created (Application.Run(new MonitorGUI());) but trying to step through after that and nothing is called. The method on form load is not even called even though I can visually see that it's loaded

5
  • 2
    Are you assigning to either of the lists in the Load event? Have they been initialized? Commented Jun 7, 2016 at 14:35
  • 3
    Did you wire any Load event like this this.Load += new System.EventHandler(this.Form1_Load);? Commented Jun 7, 2016 at 14:36
  • Neither List has been initialised and I have not wired any events Commented Jun 7, 2016 at 14:38
  • 1
    then you should. The Form1_load event is either generated by double clicking the load event property in Form window or you can manually wire up the load event. Commented Jun 7, 2016 at 14:41
  • Ah yes!! That did not help solve this problem but has helped me realise that I need to do that to prevent future ones, thank you! Commented Jun 7, 2016 at 14:43

1 Answer 1

1

Application.Run()

Begins running a standard application message loop on the current thread, and makes the specified form visible.

This method blocks and only returns when you close the Form passed as argument. So all calls after that are executed when you close your main window.

You might want to change the order:

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

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

And Form1_Load() is only called if you subscribed to the Load event of the Form:

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

But this can also be done in designer. Check if you have set this event correctly.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much!! This has done the trick. Will accept when I can :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.