0

I tried to make a while (work){....} inside button event called start to do some stuff like label changing in the same form with random values and I have another button called stop I want to make the work=false; so the while should be break


the problem is when I clicked start button and the form froze and did nothing how I can do that like stay in the while loop and access all other events

1
  • Please explain what you're actually trying to achieve, rather than just how you're trying to achieve it, because the way you're trying to achieve it isn't going to work. Commented Oct 20, 2022 at 12:18

1 Answer 1

1

As long as the loop runs, no events (like your other button's click) can be processed. This results in freezing the UI.

Better use a Timer instead of an infinite loop. The timer will not freeze the UI but call a Tick event at defined intervals and allow other events to be processed between two ticks. It is then easy for another button to stop this timer.

Since you have not mentioned any UI technology (is it WinForms, WPF, WebForms, MAUI, Xamarin, ...?) and not shown any code, it is difficult to give you example code.

Sign up to request clarification or add additional context in comments.

12 Comments

It wouldn't result in freezing the UI if not done on the UI thread.
@YungDeiza only the UI thread can update the UI. So your loop would have to post a message to the UI thread anyway, and you would not want to send to many messages, so you need some form of delay, and in the end you would just have made a very poorly implemented timer. So a standard timer is absolutely the way to go.
@JonasH The whole loop doesn't need to run on the UI thread, if only the actions that update the UI are run on the UI thread, it wouldn't be blocked. If you do not really need to do many updates very frequently then you can add a delay to the loop. Timers are more resource intensive than while loops so may not be the best solution for short-lived events.
@YungDeiza "Timers are more resource intensive than while loops", eh? no? A dedicated thread would be more resource intensive than a timer. A background thread might be motivated if there is a lot of cpu processing needed, but creating a random value is in no way intensive enough to need a background thread.
@YungDeiza I'm not sure about the internal details of timer implementations, But I would assume it is simply an entry in a OS maintained list that keeps track of all the timed work the OS needs to perform, and in the end the OS will use a hardware timer to do the actual signaling. I'm fairly sure it is not implemented as a loop on a thread, and that would not be a good option. And UI timers do not allow overlapping actions, all actions would be invoked on the UI thread, and a thread can only do one thing at a time.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.