C# 异步运行回调函数
我试图理解如何在异步控制台应用程序中运行回调函数。我的基本控制台应用程序代码如下所示:C# 异步运行回调函数,c#,asynchronous,C#,Asynchronous,我试图理解如何在异步控制台应用程序中运行回调函数。我的基本控制台应用程序代码如下所示: using Nito.AsyncEx; static void Main(string[] args) { AsyncContext.Run(() => MainAsync()); } static async Task MainAsync() { } 我希望在异步模式下运行的方法是来自websocket api的以下方法
using Nito.AsyncEx;
static void Main(string[] args)
{
AsyncContext.Run(() => MainAsync());
}
static async Task MainAsync()
{
}
我希望在异步模式下运行的方法是来自websocket api的以下方法:
using ExchangeSharp;
public static void Main(string[] args)
{
// create a web socket connection to Binance. Note you can Dispose the socket anytime to shut it down.
// the web socket will handle disconnects and attempt to re-connect automatically.
ExchangeBinanceAPI b = new ExchangeBinanceAPI();
using (var socket = b.GetTickersWebSocket((tickers) =>
{
Console.WriteLine("{0} tickers, first: {1}", tickers.Count, tickers.First());
}))
{
Console.WriteLine("Press ENTER to shutdown.");
Console.ReadLine();
}
}
上面的代码旨在锁定控制台应用程序,订阅接收数据的事件,并对接收到的数据进行处理
我要做的是在一个单独的线程或异步中运行上面的代码,这样我就可以在MainAsync()函数中继续我的代码
我在这方面的经验有限。非常感谢您的帮助 根据GetTickersWebSocket
的说法,GetTickersWebSocket不是阻塞调用
您发布的唯一阻止调用是Console.ReadLine
。
ExchangeBianceAPI
有自己的基于回调的异步,因此,只需扔掉控制台.ReadLine
,或在其前面放置更多代码即可:
static async Task MainAsync()
{
ExchangeBinanceAPI b = new ExchangeBinanceAPI();
using (var socket = b.GetTickersWebSocket((tickers) =>
{
Console.WriteLine("{0} tickers, first: {1}", tickers.Count, tickers.First());
}))
{
// code continues here
Console.WriteLine("Press ENTER to shutdown.");
Console.ReadLine();
}
}
作为旁注。
我不熟悉这一点,但源代码显示了可怜的人在WebSocketRapper中的aynchrony:
Task.Factory.StartNew(ListenWorkerThread)
// inside ListenWorkerThread
_ws.ConnectAsync(_uri, CancellationToken.None).GetAwaiter().GetResult();
result = _ws.ReceiveAsync(receiveBuffer, _cancellationToken).GetAwaiter().GetResult();
等等
有人试图以同步方式调用异步代码。相反,至少必须将
ListenWorkerThread
转换为async
方法,并且绝对不能通过Task.Factory.StartNew
调用它
如果我必须使用这个项目,我会发布一个以真正异步的方式重写代码的请求。根据GetTickersWebSocket
不是阻塞调用
您发布的唯一阻止调用是Console.ReadLine
。
ExchangeBianceAPI
有自己的基于回调的异步,因此,只需扔掉控制台.ReadLine
,或在其前面放置更多代码即可:
static async Task MainAsync()
{
ExchangeBinanceAPI b = new ExchangeBinanceAPI();
using (var socket = b.GetTickersWebSocket((tickers) =>
{
Console.WriteLine("{0} tickers, first: {1}", tickers.Count, tickers.First());
}))
{
// code continues here
Console.WriteLine("Press ENTER to shutdown.");
Console.ReadLine();
}
}
作为旁注。
我不熟悉这一点,但源代码显示了可怜的人在WebSocketRapper中的aynchrony:
Task.Factory.StartNew(ListenWorkerThread)
// inside ListenWorkerThread
_ws.ConnectAsync(_uri, CancellationToken.None).GetAwaiter().GetResult();
result = _ws.ReceiveAsync(receiveBuffer, _cancellationToken).GetAwaiter().GetResult();
等等
有人试图以同步方式调用异步代码。相反,至少必须将
ListenWorkerThread
转换为async
方法,并且绝对不能通过Task.Factory.StartNew
调用它
如果必须使用此项目,我会发布一个以真正异步方式重写代码的请求。异步方法不会阻止调用线程,如果您直接使用它,您只需在
Consolo.ReadLine()
之前的任何位置调用它,然后使用返回的Task
处理结果(如果需要)
public static void Main(string[] args)
{
// Would not block the thread.
Task t = MainAsync();
// Only if you need. Would not block the thread too.
t.ContinueWith(()=> { code block that will run after MainAsync() });
// create a web socket connection to Binance. Note you can Dispose the socket anytime to shut it down.
// the web socket will handle disconnects and attempt to re-connect automatically.
ExchangeBinanceAPI b = new ExchangeBinanceAPI();
using (var socket = b.GetTickersWebSocket((tickers) =>
{
Console.WriteLine("{0} tickers, first: {1}", tickers.Count, tickers.First());
}))
{
Console.WriteLine("Press ENTER to shutdown.");
Console.ReadLine();
}
}
如果直接使用异步方法,它不会阻塞调用线程,您可以在
Consolo.ReadLine()
之前的任何位置调用它,然后根据需要使用返回的Task
来处理结果
public static void Main(string[] args)
{
// Would not block the thread.
Task t = MainAsync();
// Only if you need. Would not block the thread too.
t.ContinueWith(()=> { code block that will run after MainAsync() });
// create a web socket connection to Binance. Note you can Dispose the socket anytime to shut it down.
// the web socket will handle disconnects and attempt to re-connect automatically.
ExchangeBinanceAPI b = new ExchangeBinanceAPI();
using (var socket = b.GetTickersWebSocket((tickers) =>
{
Console.WriteLine("{0} tickers, first: {1}", tickers.Count, tickers.First());
}))
{
Console.WriteLine("Press ENTER to shutdown.");
Console.ReadLine();
}
}
据我所知,如果您在Console.WriteLine之前添加更多代码(“按ENTER键关闭”)代码>它将已异步工作。。。由于应用程序将退出,其已在readline上暂停,这是控制台应用程序的正常过程。。如果您将更多代码放在
Console.WriteLine之前(“按ENTER键关机”),则执行填充或等待输入,或从我可以收集的内容中终止代码>它将已异步工作。。。由于应用程序将退出,其已在readline上暂停,这是控制台应用程序的正常过程。。执行任务或等待输入,或终止