I have a button that executes a command, and it contains a while loop, the application freezes after I start the command, I have also tried to run it recursively but I got the same behavior, and using a timer is not an option, the task can't be called using an interval
Asked
Active
Viewed 132 times
-2
-
2In the future, post the relevant code when asking a question – Jonesopolis Jul 30 '22 at 02:52
-
You can use Background worker and show progress bar on UI. Ref https://stackoverflow.com/questions/6481304/how-to-use-a-backgroundworker – SHAILESH BARIA Jul 30 '22 at 03:11
-
1[How do I ask a good question?.](https://stackoverflow.com/help/how-to-ask) – BionicCode Jul 30 '22 at 16:19
1 Answers
0
You have to run the blocking section of your code in a separate thread. Running it on the main UI thread will freeze the UI.
You can use Tasks
to do this.
using System.Threading.Tasks;
Task.Run(() => {
while (condition)
{
// operation
}
});

Hamid Siaban
- 346
- 4
- 13
-
2A background thread is not always the best or correct solution. It really depends on the actual code that is blocking. For example, using an async API would provide better performance. – BionicCode Jul 30 '22 at 16:19