Many consecutive Awaits causes random app delay

kskrei2kskrei2 USMember
edited December 2016 in Xamarin.Android

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:

  1. 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.
  2. 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?

Posts

  • StefanosKaratzogiannisStefanosKaratzogiannis ✭✭ GRUniversity ✭✭

    I can't exactly understand your issue but what I would do is use a cancellation token so that if the current async method is not done, it cancels

    void OnTap()
            {
                Task.Run(async () =>
                {
                    if (_TokenSource != null)
                    {
                        _TokenSource.Cancel();
                    }
                    await DoWork();
                });
            }
    
            System.Threading.CancellationTokenSource _TokenSource;
    
            async Task DoWork()
            {
                try
                {
                    _TokenSource = new System.Threading.CancellationTokenSource();
                    var work = await httpClient.GetAsync("someurl", _TokenSource.Token);
                    var res = await work.Content.ReadAsStringAsync();
    
                    var work2 = await httpClient.GetAsync("someurl2", _TokenSource.Token);
                    var res2 = await work2.Content.ReadAsStringAsync();
    
                    //...
                }
                catch (System.OperationCanceledException)
                {
                    //Probably do nothing
                }
                finally
                {
                    _TokenSource = null;
                }
            }
    
  • kskrei2kskrei2 USMember

    @StefanosKaratzogiannis Thanks for the input. However, I'm actually waiting for the tasks to finish before starting a new one. So I call OnTap(), it completes, wait 10 seconds, call onTap() again, etc. So OnTap is never called while OnTap() is running.

    I'm not sure why the tasks take so long to finish or if its just a delay in writing to the log
    [Mono] [0xb97b07f0] worker finishing
    Thread finished: #26
    Thread finished: #6

  • GeorgeCeaserGeorgeCeaser USMember

    Any update on this? I have similar problem. Any information would be appreciated.

Sign In or Register to comment.