If I have something like while (true) Application.DoEvents(); (it’s really not exactly like that) and when I close the main form, the program stays running. How can I make the program stop running? Is there any variable like 'MustTerminate' that I can check in my while loop to know when to finish it? On real life the loop is waiting for a web page to load. I could put a timer but I don’t want the program to wait for the timer to stop running if when I close the form.
If I have something like while (true) Application.DoEvents(); (it’s really not exactly like that)
Share
Yes, this is the reason you’ll always get advice to not use DoEvents(). It is far to indiscriminate about what kind of events it allows to run. Not just closing forms, pressing the button that starts that loop is a nasty problem as well.
Nevertheless, there is already code in .NET that uses DoEvents() in a loop. Form.ShowDialog(). It makes it safe by doing the equivalent of this:
Setting the Enabled property to false is what makes it safe. The user can’t close forms or start the command again.