API request WaitingForActivation “Not yet computed” error

老子叫甜甜 提交于 2019-12-20 16:32:49

问题


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.


回答1:


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.




回答2:


This looks like a classic async-await deadlock: you have a SynchronizationContext, you await in it (which means the continuation is scheduled to that SynchronizationContext) and then you block that SynchronizationContext by calling Wait(), which leads to deadlock.

The right solution to not block on async code, your Main should be an async method that returns a Task and awaits balance_task. Another option is to use ConfigureAwait(false) in getLatestWalletAmounts() (and any other "library" method that uses await).



来源:https://stackoverflow.com/questions/37129427/api-request-waitingforactivation-not-yet-computed-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!