The following code abstracts away my actual task, which is to go through each pixel in a bitmap.
myButtonCommand.execute(async () => {
await Task.Run(() => {
while(i++<1000000) {
// do some work
if(i % 1000 == 0) // only update on every 1000th iteration
{
ProgressChanged.Invoke(this, i); // fire update event
Thread.Sleep(n);
}
}
});
});
The lambda that is supplied to myButtomCommand.execute
runs on the UI thread. But since Tasks aren't guaranteed to run on the UI thread doesn't that mean that calling Thread.Sleep(n)
could possibly freeze the UI thread?
The underlying problem is that I need to update a progress bar and without doing a Thread.Sleep(n)
the UI thread gets blocked while the Task is spinning.
Now, what I ended up doing was using Thread.Sleep(0)
so it will only go to sleep if another thread needs to run. But I don't understand why using Thread.Sleep(n)
is necessary in the first place since, at least in my tests, it was on a separate thread anyways.
So I'm confused about two things:
- Is it safe to call
Thread.Sleep(n)
from a Task off the UI thread? - In my example, why is
Thread.Sleep(n)
necessary to get the UI to update given the Task ran on a separate thread?