Reputation: 155
In a windows desktop application (windows forms), I've created a while loop in (c#):
bool stop = false;
while (!stop){
...
// code to update a label that displays the number of times the loop executed.
...
}
When a button is clicked, "stop" is made true and the loop will stop. However, I don't get the chance to click the button after the application runs because it becomes unresponsive.
that's one thing. The other thing that puzzles me more is why the label is not showing the number of loops. It's like the program is just looping and doing nothing.
If you are wondering, I'm trying to write code for a game loop.
Upvotes: 1
Views: 1295
Reputation: 887449
Windows applications work by responding to Windows messages.
All WinForms events actually come from Windows messages like WM_PAINT or WM_MOSUEMOVE.
The .Net Framework's Application
class runs a message loop which asks Windows for the next message, then processes the message as appropriate.
For example, if the user clicks a button, Windows sends your program a WM_CLICK message. .Net's message loop converts this into a Click
event and runs any event handlers that you registered.
While your code is running, the application is busy responding to whatever message it received, and it cannot respond to messages.
Therefore, it appears frozen.
You should replace your loop with a Timer
component.
Move the loop body into the Tick
handler and set the Interval
to something like 20
or 50
.
Upvotes: 2