Reputation: 6234
I have simple code which I want to be executed asynchrounously:
public async Task EncryptAsync()
{
for (int i = 0; i < 10; i++)
{
// shift bytes
// column multiplication
// and so on
}
}
That is the way I call above method:
private async void Encryption()
{
ShowBusyIndicator();
await encryptor.EncryptAsync();
HideBusyIndicator();
}
The point is, when I add await Task.Delay(1000);
in this method, busi indicator is shown, but after 1 sec, application locks and waits SYNCHRONOUSLY for encryption complete. If I remove Task.Delay, application lock and unlocks after operation finished.
How to use properly await and asyc in my case? I want simply execute method encryptor.EncryptAsync()
asynchronously.
Upvotes: 1
Views: 271
Reputation: 888077
The async
/ await
keywords do not allow you to create new asynchrony; instead, they simply allow you to wait for an existing asynchronous operation to complete.
You want to run your CPU-bound operation on a background thread to avoid blocking the UI.
To do that, call the synchronous blocking method inside a lambda passed to Task.Run()
.
You can then use await
to wait for the resulting asynchronous operation (a Task
instance).
Upvotes: 4