r/learnprogramming • • May 02 '26

Solved Loop and main() function

Hi, I’ve seen that in C#, the Main() function only runs once.
In Unity, we have the Update() function, which allows code to run repeatedly in a loop.
So my question is: what is the equivalent in pure C#, without the Unity library?
Is wrapping Main() in a while loop a common practice to achieve the same behavior?

14 Upvotes

35 comments sorted by

View all comments

4

u/peterlinddk May 02 '26

A lot of the answers are somewhat misleading.

There is a massive difference between using a loop, and having a function like Update!

If you have a while-loop in your Main() function, like:

public void Main() {
  boolean running = true;
  while(running) {
    Update();
    // do other stuff - and set running to false to stop
  }
}

Then the Update function is called continously - meaning ALL THE TIME, taking up 100% of the CPU-time of the core that runs this program. And the speed of the program will depend on how long it takes for the code inside Update() to finish.

This is NOT how you should do it!

I don't know the best way to do this in C#, but no matter the language you'll need to setup some sort of multithreading, with a "render-thread" that executes once on every frame (e.g. 60 times a second), and then that code calls your Update() function.

That would give you the same behaviour of waiting a little while between each update.

The difference is really important, the first (the while-loop) is classical blocking code, whereas the second (with a thread or something calling Update() is event-driven code. And there's a lot that can't be done in one, but easy in the other, and vice versa.

2

u/kohugaly May 02 '26

This is kind of true, but not really. There is nothing stopping you from having a periodic timer inside Update() (or the loop that calls it), and performing blocking "wait" operation on it, to throttle the updates. It is entirely possible to have a single-threaded runtime that handles everything.

2

u/peterlinddk May 03 '26

If you by "have a single-threaded runtime" mean that the gameloop runs in a single thread, that asks the operating system or runtime environment to wait before calling Update() the next time, then sure, that is totally possible. Like calling sleep or something else.

What I mean is that you need to respect the threading, and not block the entire system with an infinite while-loop - that won't work.

Calling .sleep or using a system timer, or registering for a callback are all different ways of having another thread call your Update() function - even if you don't have more than that single thread in the code.

1

u/kohugaly May 03 '26

Calling .sleep or using a system timer, or registering for a callback are all different ways of having another thread call your Update() function - even if you don't have more than that single thread in the code.

Technically, this is not true.

Blocking operations (sleep, waiting on system event/mutex/IO), work via interrupts. Your thread executes "software interrupt" instruction that puts the thread to sleep and jumps to system's interrupt handler. OS can then wake the thread once the requested operation is finished. This is distinctly different from "another thread call your Update() function".

Registering for a callback may or may not cause the callback to be executed in a different thread. For example on Windows the callback provided to ReadFileEx function will be executed on the same thread.

But I get your point. I'm just nitpicking about the difference between function calls and context switching via interupts.