Binance API调用可以在控制台应用程序上正常运行,但不能在WinForm c#上运行 [英] Binance API call working fine on Console Application but not on WinForm c#

查看:270
本文介绍了Binance API调用可以在控制台应用程序上正常运行,但不能在WinForm c#上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在使用的Github存储库 https://github.com/ilkerulutas/BinanceSdk

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

该代码在控制台应用程序上可以正常工作,但是在Windows窗体应用程序中获得响应会花费太多时间.

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;

我同时调用了两者,但是async方法的状态为 WaitingForActivation ,而Sync方法的时间却太多,无法获得响应.

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

我将这两种方法称为

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

推荐答案

您正在死锁UI线程.

我的猜测是您正在调用TimeSync(),然后调用Task.Result,这会阻塞UI线程,直到从Time()返回的Task完成.

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.

由于Time()中有一个在UI线程上启动的await,因此一旦请求完成,异步回调将安排在UI线程上发生,但是此UI线程已经通过等待Task.Result.因此,您处于僵局.

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.

您能说明一下如何使用TimeSync以及为什么要使用Sync方法,然后再使用async方法.要对其进行正确修复,您需要使呼叫链中的所有组件保持异步状态,然后等待结果!

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!

您可以尝试做一个补丁修复(此补丁可能有效或无效,最好的情况是在通话过程中它仍会阻止UI):

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;

这篇关于Binance API调用可以在控制台应用程序上正常运行,但不能在WinForm c#上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆