Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

API request WaitingForActivation "Not yet computed" error

Tags:

c#

async-await

I'm using the Poloniex C# API code from: https://github.com/Jojatekok/PoloniexApi.Net

On my console application the request to get balances is working, i make the API call and the balances come back, but on my Windows Forms application it's not getting past waiting for the balances:

Id = 14, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"

when using this code:

PoloniexClient polo_client { get; set; }

private void Main(){
    polo_client = new PoloniexClient(ApiKeys.PublicKey, ApiKeys.PrivateKey);
    var balance_task = getLatestWalletAmounts();
    balance_task.Wait();

    // doesn't get past here on my Forms app

    if (balance_task.IsCompleted){
          // gets to here on my Console app
    }
}

// get wallet and startup amounts
async Task<IDictionary<string, Jojatekok.PoloniexAPI.WalletTools.IBalance>> getLatestWalletAmounts()
{ 
   // get wallet coin list 
   return await polo_client.Wallet.GetBalancesAsync();  
}

It's the exact same code as my Console application, but on the Forms app the task is not completing and on the Console application it is completing and i'm getting back:

Id = 3, Status = RanToCompletion, Method = "{null}", Result = "System.Collections.Generic.Dictionary`2[System.String,Jojatekok.PoloniexAPI.WalletTools.IBalance]"

Any hints as to why the same request isn't completing on my Forms application but it is on my Console app? I'm using the exact same Poloniex C# project referenced in my Console and Forms app to interact with the API.

The API public key is the same in both projects and the API private key is the same in both projects.

like image 388
Nickmccomb Avatar asked May 10 '16 04:05

Nickmccomb


1 Answers

You cannot mix async and synchronous code like this. By calling .Wait, the UI thread is stuck waiting for the task to finish, but the task is essentially trying to "Invoke" on the UI thread, so it cannot finish. Result: deadlock.

You can see more information about the basic problem here.

One option, as a band-aid, is to use ConfigureAwait(false) on the await polo_client.Wallet.GetBalancesAsync() call; that will override the default behavior of trying to return to the UI thread. (Note that means you can't access the UI after the await, because it will be continuing on a different thread!)

I have written a longer piece here about bringing async code into the core of a UI application.

like image 128
Mark Sowul Avatar answered Sep 28 '22 15:09

Mark Sowul