0

I have a little problem with async/await methods in a C# app for Windows Phone 8.1

I am creating a simple app for getting data from my service and save it on an user device.

I have got this code:

public static class ScheduleParser { public static async Task<string> GetSchedule(string groupNumber) { string siteAddress = "site.com"; siteAddress = string.Format(siteAddress, groupNumber); var client = new HttpClient(); var response = await client.GetStringAsync(siteAddress); return response; } ... } 

I use it in this way:

 private async void ButtonBase_OnClick(object sender, RoutedEventArgs e) { var schedule = await ScheduleParser.GetSchedule("1111111"); } 

I debug my app on a real device. When GetSchedule is called, I see that it works well and returns a value. But I don't receive the value in ButtonBase_OnClick.

How can I fix this?

4
  • HttpClient should be wrapped in a using so that it gets disposed when you're done. Commented Jan 9, 2015 at 17:10
  • What do you mean by But I don't receive the value in ButtonBase_OnClick. ? Commented Jan 9, 2015 at 17:11
  • ScheduleParser.GetSchedule("1111111") don't return any value Commented Jan 9, 2015 at 17:14
  • For a example, If I write code like this var schedule = await ScheduleParser.GetSchedule("1111111"); doSomething() doSomething won't be called. Commented Jan 9, 2015 at 17:19

1 Answer 1

2

In the code that you showed you aren't actually using the result. schedule is an unused variable. Since the compiler can prove the value can never be read, it doesn't bother to create the variable in the first place.

When you actually use the value then you'll be able to see the result of the method call.

Sign up to request clarification or add additional context in comments.

5 Comments

@I3arnon No, not only in release mode. It's more aggressive in release mode, but it's an optimization that can also be taken in debug mode.
Everything in release mode can be done in debug mode. It's all just msbuild targets at the end. By default, it's a distinction between release mode and debug mode.
AFAIK this optimization will not be done in debug mode as it will make your debugging session painful. Also seen hans passant said somewhere in stackoverflow, can't find the link now.
@SriramSakthivel I've personally seen it happen in debug mode.
@Servy May be you turned on optimize code checkbox in debug mode also? That will be turned off by default. (Edit: corrected typo)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.