Trying to deal with asynchronous code execution. Took example from MSDN and simplified a little:
private async void button1_Click(object sender, EventArgs e) { int i = await emuAccessTheWebAsync(); label1.Text += i.ToString(); } async Task<int> emuAccessTheWebAsync() { Task<string> t = new Task<string>(() => { Thread.Sleep(3000); return "completed"; }); DoIndependentWork(); string urlContents = await t; return urlContents.Length; } After 3 seconds, as indicated inside the task, nothing happens. I tried to output the result inside the task, but when accessing label1 I get, of course, a cross-thread exception.
How to get the result out of the asynchronous method correctly? And is it possible to transfer some intermediate results to it from the main thread?