Officer Jonez
Officer Jonez

Reputation: 253

Weird issue with await operator

I had a weird problem today... My code is running stuck on the await method in c# (no error or sth).

user = await JsonConvert.DeserializeObjectAsync<User>(content);

but when i remove the "await" my code works...

user = JsonConvert.DeserializeObjectAsync<User>(content).Result;

I have the same problem with my HttpClient:

Works

using (HttpResponseMessage response = client.PostAsync(url, posts).Result)

Doesn't work

using (HttpResponseMessage response = await client.PostAsync(url, posts))

the problem wasn't there the day before and if i place my code in another class it works properly

output:

The thread 0xe44 has exited with code 259 (0x103).
The thread 0x81c has exited with code 259 (0x103).
The thread 0x150c has exited with code 259 (0x103).
The thread 0x1660 has exited with code 259 (0x103).

I rebuilded and I have still that problem.

Does someone know the answer?

Upvotes: 2

Views: 1382

Answers (1)

Stephen Cleary
Stephen Cleary

Reputation: 456647

You are running into a common deadlock issue that I describe on my blog.

To solve it, use await all the way; remove any Task.Wait or Task<T>.Result calls from your code.

Upvotes: 5

Related Questions