Task never ends

  • Thread starter Thread starter Heiko65456465
  • Start date Start date
H

Heiko65456465

Guest
Hi,

Inside Page_Loaded(), an async method Caluculate() is called, deeply nested over a constructor. Since the call stack contains a class constructor, I cannot use await, but want to wait for the result with 'Task.Result'.

Within Calculate() there is a task that is processed completely. However, this task itself is never terminated, i.e. 'return res;' within 'Calculate()' is never reached.

The whole thing can be easily reproduced with the following example:

public sealed partial class MainPage : Page
{
private void Page_Loaded(object sender, RoutedEventArgs e)
{
MyClass obj = new MyClass();
}
}

internal class MyClass
{
private string A;

internal MyClass()
{
A = Calculate().Result;
}

internal static async Task<string> Calculate()
{
string res = "";

await Task.Run(() =>
{
res = "abc";
});

return res;
}
}

How can I solve the problem?

Heiko

Continue reading...
 
Back
Top