简体   繁体   中英

Binance API call working fine on Console Application but not on WinForm c#

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM