In my Android app, I have many consecutive awaits calling various System.Net.Http.HttpClient GET's and POST's. If I tap a button in the application to call the method which executes these tasks, every one in roughly 15 times I get a random delay in execution.
Basically the code looks like this
void OnTap() {
Task.Run(async () => {
await DoWork();
});
}
async Task DoWork() {
await httpClient.GetStringAsync(url);
await httpClient.GetStringAsync(url2);
//More await calls
}
Usually the task runs in 1-2 seconds. However, if I call the OnTap() method repeatedly after each try, once in awhile it takes roughly 10 seconds to finish.
Here are my findings:
- Most of the code is shared between iOS and Android including everything in and past OnTap(). The random delay does not occur on iOS. Only Android.
- My guess is this has something to do with threading/tasks. When the extra delay happens, I can see the following in the logs:
[2016-12-25T16:15:33.2478090-06:00] [debug] Attempting to send GET to:
Thread finished: #17
//After 10 second delay
Thread started: #35
[2016-12-25T16:15:43.2865390-06:00] [debug] Got response back from
I have verified that the actual request isn't getting sent out and the app is "doing nothing" for those 10 seconds.
And then, if I let the app sit for a minute or so after the delay happens, I see these messages trickle in every 5-20 seconds or so.
[Mono] [0xb94793a0] worker finishing
Thread finished: #4
Thread finished: #27
[Mono] [0xb97b07f0] worker finishing
Thread finished: #26
Thread finished: #6
[Mono] [0xb96e5340] worker finishing
[Mono] [0xb98643a8] worker finishing
Thread finished: #9
Thread started: #29
Thread started: #30
[Mono] [0xb94e17c8] worker starting
Thread started: #31
[Mono] [0xb97dfe00] worker starting
Thread finished: #31
[Mono] [0xb97dfe00] worker finishing
Thread finished: #29
Thread finished: #30
[Mono] [0xb94e17c8] worker finishing
I'm not sure why these threads takes so long to finish since the http requests finished quite some time ago.
Does anyone have an idea on what's happening here and how I can get around this seemingly random delay? Should I use a different HTTP client? Set some property on the thread pool?