-2

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

Hamid Siaban
  • 346
  • 4
  • 13
haidar Dif
  • 27
  • 5

1 Answers1

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
  • 2
    A 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