Binance API call working fine on Console Applicati

2019-09-01 09:01发布

问题:

This is the Github Repository which I am using https://github.com/ilkerulutas/BinanceSdk

The code is working fine on a console application but it takes too much time to get a response in windows form application.

//Async Method

public async Task<TimeResponse> Time()
    {
        return await SendRequest<TimeResponse>("time", ApiVersion.Version1, ApiMethodType.None, HttpMethod.Get);
    }

  //Sync Method

    public TimeResponse TimeSync() => Time().Result;

I am calling both but async method is giving status WaitingForActivation and Sync method is taking too much time to get response.

I am calling these 2 methods as

var timeResponseAsync = publicRestClient.Time();  //For Async
var timeResponseSync1 = publicRestClient.TimeSync();  // For Sync

回答1:

You are deadlocking your UI thread.

My guess would be that you are calling TimeSync() which then calls a Task.Result, which blocks the UI thread until the Task returned from Time() is finished.

As you have an await inside Time() which starts on the UI thread, the async callback once the request is complete will be scheduled to happen on the UI thread, but this UI thread is already blocked by waiting for the Task.Result. So therefore you are in a deadlocked situation.

Could you show how you are using TimeSync and why are you using a Sync method which then uses an async method. To properly fix it, you need to make everything up the call chain async and await the result!

One patch fix you can try is to do (this may or may not work, best case scenario it will still block the UI for the duration of the call):

public TimeResponse TimeSync() => Task.Factory.StartNew(() => Time()).Result;