I trying get a string to set three label text asynchronously, Firstly I tried with this
private async void button1_Click(object sender, EventArgs e)
{
label1.Text = await SetTextbox(3);
label2.Text = await SetTextbox(2);
label3.Text = await SetTextbox(1);
}
private async Task<string> SetTextbox(int delaySeconds)
{
Thread.Sleep(delaySeconds * 1000);
return "Done.";
}
As you can see I try to add some delay in every call to know what label text should be set first, but this code doesn't work, the GUI still freezing and the processes still being synchronous.
For now I make it work using Task.Run:
private async void button1_Click(object sender, EventArgs e)
{
var proc1 = Task.Run(async () => { label1.Text = await SetTextbox(3); });
var proc2 = Task.Run(async () => { label2.Text = await SetTextbox(2); });
var proc3 = Task.Run(async () => { label3.Text = await SetTextbox(1); });
await Task.WhenAll(proc1, proc2, proc3);
}
but something tells me that this is not right way to achieve this, Can you tell me what would be the best way to do this or this is good solution?