Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Task Status WaitingForActivation

I have the following function :

async public Task<bool> checkNetwork (RestClient _client, ConnectivityManager conn)
    {
        var connected = false;
        var activeConn = conn.ActiveNetworkInfo;
        if (activeConn != null && activeConn.IsConnected) {
            var request = new RestRequest ();
            request.Timeout = 5000;
            var response = await _client.ExecuteAwait (request);
            //Task<IRestResponse> tmpResponse = _client.ExecuteAsync (request);
            if (response.ErrorException != null)
                connected = false;
            else
                connected = true;
        }

        return connected;
    }

It keeps hanging because the status never leaves WaitingForActivation when I call it. Any ideas?

like image 989
user3846013 Avatar asked Dec 11 '22 04:12

user3846013


1 Answers

It keeps hanging because the status never leaves WaitingForActivation when I call it.

Actually, the WaitingForActivation is just an indication that the task has not yet completed. It's not the cause of the hang, but an indicator of it. Promise Tasks (including tasks returned by async methods) will stay in the WaitingForActivation state until they complete; I describe the task state machine in more detail on my blog.

The most likely cause of a deadlock is calling Task<T>.Result or Task.Wait further up the stack. I describe this deadlock in full on my blog, but the gist of it is that await will capture a "current context" and use that to resume its async method. If called from the UI thread, then that context is the UI thread, and if the UI thread is blocked by calling Result/Wait, then the async method cannot resume on that UI thread.

like image 124
Stephen Cleary Avatar answered Dec 17 '22 20:12

Stephen Cleary