Monday, July 29, 2013

Crash when calling Task.Result from UI-Thread

Hi,


i'm wondering why my application shows unexpected behavior in certain cases. I tracked down the problem to the following simple scenario:


The threads seems to disappear if i call Result from inside the UI Thread. To reproduce the problem create a blank app and replace the OnNavigatedTo method of the Mainpage with the one below.




/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
bool result = DoSomething().Result;
result = false;
}

private async Task<bool> DoSomething()
{
await Task.Delay(1000);
return true;
}

Can anybody tell why this happens? And how it's done correctly?


Thanks



No comments:

Post a Comment