Wait 5 seconds in Universal App

lostluke picture lostluke · Jan 28, 2016 · Viewed 12.9k times · Source

I need to make a pause in a Windows 10 UWP App.

And the only thing i want is to wait 5 seconds to do the next action. I tried Task. Sleep but then the pressed button was frozen...

Pause should be here:

loading.IsActive = true;

   //int period = 5000;
   //ThreadPoolTimer PeriodicTimer =
   //ThreadPoolTimer.CreatePeriodicTimer(TimeSpan.FromMilliseconds(period));

loading.IsActive = false;

How can I make a 5s pause?

Answer

croxy picture croxy · Jan 28, 2016

You could use the Task.Delay() method:

loading.IsActive = true;
await Task.Delay(5000);
loading.IsActive = false;

When using this method your UI doesn't freeze.

Edit
A more readable way IMO would be to don't pass the milliseconds as parameter like in the above example. But instead pass a TimeSpan instance:

await Task.Delay(TimeSpan.FromSeconds(5));