2

I'm developping on Windows phone 8 and I would like to know if it's possible to manipulate data in the same method when we call DownloadStringCompleted of WebClient?

private void DownloadDataFromWebService(String uri)
    {
        WebClient wc = new WebClient();
        wc.DownloadStringAsync(new Uri(uri));
        wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
    }

    private void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        RootObject r = JsonConvert.DeserializeObject<RootObject>(e.Result);
        List<Category> listeCategories = r.Result;
    }

Thus, I would like to manage all code in only one method because I would like to return an object For example,

private List<Category> GetCategories(String uri)
    {
        WebClient wc = new WebClient();
        wc.DownloadStringAsync(new Uri(uri));
        .....
        .....
        RootObject r = JsonConvert.DeserializeObject<RootObject>(e.Result);
        return (List<Category>) r.Result;
    }
Volkan
  • 686
  • 1
  • 9
  • 17
  • you might take a look at the "new" asynchronous programming techniques (async/await) http://www.developer.nokia.com/Community/Wiki/Asynchronous_Programming_For_Windows_Phone_8 – polkduran Mar 11 '13 at 13:47

1 Answers1

2

Yes, that is possible, due to magic TaskCompletionSource class, http://msdn.microsoft.com/en-us/library/dd449174(v=vs.95).aspx . To download:

async Task<List<object>> getCategories(String uri)
{
    var taskCompletionObj = new TaskCompletionSource<string>();
    var wc= new webClient();
    wc.DownloadStringAsync(new URI(uri, Urikind.Absolute)) += (o, e) =>
    {
    taskCompletionObj.TrySetResult(e.Result);
    };
    string rawString = await taskCompletionObj.Task;
    RootObject r = JsonConvert.DeserializeObject<RootObject>(rawString);
    return (List<Category>)r.Result; 
}

To use: var x = await getCategories(myURI);

Bianca Daniciuc
  • 920
  • 1
  • 13
  • 22
  • Thank you for your answers. But now, how can I show the result of x? because when I show it in console, I have : System.Threading.Tasks.Task`1[System.String] I need the result because I would like to convert a JSON to a object – Volkan Mar 11 '13 at 15:00
  • Well, since `downloadString` method returns Task this means that in the end, x will be a string. So x is your string that just came from the internet. Did you tried something like x.Result or I've just misunderstood it? Could you show me a stackTrace? – Bianca Daniciuc Mar 11 '13 at 15:17
  • when I add "await" in front of downloadString method => var x = await downloadString(myURI); I must change the type of my method to Task. But what I would like is return a List. So I removed the word await, and when I display the variable x, instead of displaying a JSON code, it displays me "System.Threading.Tasks.Task`1[System.String]" And when I do x.Result, thus " var x = DownloadString(new Uri(stringBuilder.ToString())); Debug.WriteLine(x.Result);" The app crashes and there is no error... – Volkan Mar 11 '13 at 15:41
  • In fact, the problem comes from "var x = await getCategories(myURI);" when I do this in a method, I must change the type of my method... – Volkan Mar 11 '13 at 16:08
  • Yes, your calling method should be async too. As a rule you can consider every call of await.. should be made in an async method. – Bianca Daniciuc Mar 11 '13 at 16:16
  • And how can I call this method in a constructor? – Volkan Mar 11 '13 at 16:17
  • Well that's a problem, since constructor shouldn't be async. See this [question](http://stackoverflow.com/questions/8145479/can-constructors-be-async). But that is another subject. A solution would be to construct your object, and init it later, in an async init. – Bianca Daniciuc Mar 11 '13 at 16:21
  • Thank you, I look on this side and I will keep you posted – Volkan Mar 11 '13 at 16:35
  • Hello, so, I do it in a method instead of constructor and now it works :) thanks ana – Volkan Mar 13 '13 at 08:26