0

I am making a "Main()" function in a WindowsForms Application in C#. I have been following a book on Game programming in C#. When I run the examples everything works, but when I try to make my own version nothing works.

Here's the Main() function:

    public void Main()
    {
        while (!gameOver)
        {
            // Non timed code:           
            int ticks = Environment.TickCount;
            if (ticks > lastTick + 16)
            {
                lastTick = ticks;
                //Timed (60FPS) code here:
                Application.DoEvents();  
            }
        }
    }

When I put this inside the "Form1_Load" function the form does not even show when I start the program, while not giving any errors or warnings (the same thing is done in the examples, which runs). If I move my "Main()" to for example "MouseClick" and the form shows and when I click is the function starts running as it should.

I am really out of ideas as to why this happens. Am I missing something really obvious here?

Nathan A
  • 11,059
  • 4
  • 47
  • 63
Michael
  • 175
  • 1
  • 1
  • 12

2 Answers2

2

A form's Load event is not exactly the best place to start a game-loop. There are two basic reasons that Load will be fired. The "good" way is when it happens in response to the Show() call, normally present in the Program.Main() method. Your game-loop will work.

The "bad" way is when code in your form constructor requires the form's Handle property to be valid. That forces Winforms to create the native window and that triggers Load. That still usually comes to a good end, the odds get lower the more convoluted it gets.

That will go wrong in your case since the Load event handler doesn't return. Which means that the constructor cannot complete. Which means that the window cannot become visible. Which means that "gameOver" can never become true. Game over. You diagnose this with the debugger, set a breakpoint on the Load event handler and look at the Call Stack window. With the expectation that you'll see the statement in the constructor that caused the problem.

Last but not least, be very wary of this failure mode.

The real fix is to put this code in the right place. Which is in the Program.Main() method. Roughly:

    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        // Application.Run(new Form1());   Not this anymore
        var window = new Form1();
        window.Show();
        // Your game loop here
        //...
    }
Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

You could try putting the loop logic in a static void method and executing it on a thread

that way it will go about its merry way and the form will go ahead and load

China Syndrome
  • 953
  • 12
  • 24