0

So I have an app that is retrieving web information. After I get this information I need to process the object returned (A JSON string). However, as far as I can tell, my program keeps breaking as when the web info is awaiting, it jumps into the next processing stage, which has no data to process, therefore breaks. How can I go about fixing it?

Here is the async method

    async private Task GetInformation(string url)
    {
        client = new HttpClient();

        response = await client.GetAsync(new Uri(url));
        result = await response.Content.ReadAsStringAsync();
    }

As mentioned below, I should have put await infront of the location where I am calling GetInformation, however, that method is used to return a string to another class like so

    public string GetResult(string url)
    {
        GetInformation(url);
        return result;
    }

How can I fix this?

Thanks for any help

2
  • 1
    Maybe you can put a time limit for waiting response? Commented Nov 12, 2013 at 12:03
  • consider using ConfigureAwait(false) on task Commented Nov 12, 2013 at 12:18

2 Answers 2

3

The place where you call GetInformation(url) must also have the await keyword:

// stage1
//... some code ...

// stage2
await GetInformation(url);

// stage3
//... some code ...

Without await your method launches the download but it runs in parallel with stage 3.

An alternative to the await for when you can't make that method async is to launch the task in another thread and wait for that thread to complete:

Task.Run(() => GetInformation(url)).RunSynchronously(); // use .Result if you have Task<T>

Task.Run() can be skipped if your code does not run on the UI thread - then just call GetInformation(url).RunSynchronously(). If it does run on UI thread then without Task.Run you risk deadlocks.

Your third option would be to use a locking mechanism like ManualResetEvent. If the previous approaches do not work, look for samples using this class.

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

4 Comments

And of course method calling GetInformation also has to be async
Okay that's great, however the method I call GetInformation is being used to return the result to another class. I updated the OP to show what I mean
@AndyOHart - that does not change anything - make that method async, then the method calling it etc. If you can't do that, I added an alternative to the answer.
I made GetResult async and had it return a type of Task<string>, however, when I try set the string to this, it says it can't be a type of Task.string.
1

Use following code

private async Task<string> GetInformation(string url)
{
    client = new HttpClient();

    response = await client.GetAsync(new Uri(url));
    return await response.Content.ReadAsStringAsync();
}

And use as following:

var task = GetInformation(url);
DoSomeStuffInParallelWithDownload();
var result = task.Result;
UseDownloadResult(result);

1 Comment

await and task.Result does not quite go together - your code will not compile...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.