a small application that is running in an infinite loop
That is your problem, the while(true) { ... }
loop blocks processing of Windows messages. And that makes your app blind and deaf.
A quick and very dirty fix would be to add an Application.DoEvents()
call after the Sleep()
but this is not the right way to go. Your OnExit()
could then happen while the loop still runs...
Windows is an event-driven OS, so write event-driven solutions. Without using Sleep()
or DoEvents()
.
In your case you should probably be using a Timer and probably a BackgroundWorker or other Thread solution.