Question

I made an app with an API that allowed me to create my own event loop. I’m trying to get that same effect with the .NET forms to convert this application. It basically looked like this:

int main()
{
    InitializeComponents();
    While(!quitting()){
        updateComponents();
        renderComponents();
    }
    terminateComponents();
    return 0;
}

Is it possible to access the main loop of a form?

Or is there an event which is triggered every frame? With this I can, for example, calculate the frames per second. This event does not have to be called by Form, but maybe even by System, so you can even use them with services, applications and for the web.

Thanks

Was it helpful?

Solution

When nothing happens in your program it does nothing, so in an idle application there are no real frames.

And that's why there is the Application.Idle event. It happens every time the application finishes doing whatever it has to do.

Something like that:

//somewhere in the initialization function:
Application.Idle += OnIdle;

void OnIdle(object sender, EventArgs e)
{
    updateComponents();
    renderComponents();
}

NOTE: According to the documentation you have to detach it somewhen in the future:

//finalization function
Application.Idle -= OnIdle;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top