Could I please ask for help in understanding how to use Async\Await to help keep a page more responsive?
I have read the accepted reply here: If async-await doesn't create any additional threads, then how does it make applications responsive? and I thought I understood it.
I have a page with a button and a checkbox. The checkbox does nothing, it is just there for me to check and uncheck. I want to start a long running function on the button press and keep the UI responsive so that I can check and uncheck the checkbox to prove that the UI is still responsive. Should that be possible with Async/Await?
I have a long running function like so (takes about 4 seconds to complete):
async BigFunction()
{
var r = 0;
for(var i = 0; i < 10000; i++)
{
for(var j = 0; j < 1000; j++)
{
for(var k = 0; k < 1000; k++)
{
r = Math.sin(4);
}
}
}
return r;
}
My on button pressed hander is as so:
OnPressed(): void {
(async () => {
await this.BigFunction();
return 1;
})();
}
I thought that this would be enough (from my understanding of the post in the above link) to keep the UI responsive. It is not. My checkbox is unresponsive while the long running function executes.
Thanks for any help with a solution.
Here is a passage from the above link that made me think this would work:
What the await keyword, together with some clever compiler magic, does is that it basically something like "Ok, you know what, I'm going to simply return from the button click event handler here. When you (as in, the thing we're waiting for) get around to completing, let me know because I still have some code left to execute". So it returns back to the message loop, which is now free to continue pumping messages, like moving the window, resizing it, or clicking other buttons.