我有这样的简单代码。我收到消息处理缓慢的警告,并考虑将数据处理卸载到另一个线程。
所以我试图以异步方式调用 HandleBuy/SellOrder 函数。我怎样才能做到这一点?因为这些方法不返回任何东西。我只是希望他们以异步方式做他们的事情。执行此操作的最有效方法是什么?
让我困惑的是,如果我将方法标记为异步,IDE 说因为我不使用 await,它们将同步运行。但是如果我等待这意味着线程将等到函数完成?因为我不想那样。我希望这些函数在另一个线程中完成它们的工作,而不会阻塞主线程。
此外,如果我将方法标记为异步,那么我必须返回一个任务吗?因为我没有任务要返回。只是一些需要在后台进行的操作。
socketClient.Spot.SubscribeToUserDataUpdatesAsync ( spotListenKey.Result.Data,
// Order update
data =>
{
// Order update
string symbol = Utility.GetBaseSymbol ( data.Symbol );
if ( data.Side == OrderSide.Buy )
Utility.HandleBuyOrder ( data );
else if ( data.Side == OrderSide.Sell )
Utility.HandleSellOrder ( data );
},
// OCO updates
null,
// Position updates
null,
// Balance updates
null
);
public static void HandleBuyOrder ( BinanceStreamOrderUpdate data )
{
if ( data.Status == OrderStatus.Filled || data.Status == OrderStatus.Canceled || data.Status == OrderStatus.New )
{
if ( data.QuantityFilled > 0 )
{
var pairSymbol = Utility.GetPairSymbol ( data.Symbol );
if ( pairSymbol != "" )
MarketData.UpdateStableCoinBalance ( pairSymbol );
string order = String.Format ( "{0}\t{1}\t{2}\t{3}\t{4}\t{5}", data.Symbol, data.Price, data.QuantityFilled, data.QuoteQuantityFilled, data.OrderId, data.EventTime );
Utility.AddCoinToOrderLogs ( order, data.OrderId );
var newAction = new UpdateAction ( UpdateType.NewCoinAdded, data.Symbol );
Utility.UpdateActions.Enqueue ( newAction );
Console.WriteLine ( order );
}
}
}
public static void HandleSellOrder ( BinanceStreamOrderUpdate data )
{
if ( data.Status == OrderStatus.Filled || data.Status == OrderStatus.Canceled || data.Status == OrderStatus.New )
{
if ( data.QuantityFilled > 0 )
{
var pairSymbol = Utility.GetPairSymbol ( data.Symbol );
if ( pairSymbol != "" )
MarketData.UpdateStableCoinBalance ( pairSymbol );
string symbol = Utility.GetBaseSymbol ( data.Symbol );
var price = Utility.Coins.FirstOrDefault ( c => c.Symbol == symbol )?.Balance.Price;
var balance = GetBalanceFromAccount ( symbol );
if ( balance.AvailableAmount * price < 1 )
{
Utility.RemoveCoinFromBuyOrderLogs ( symbol );
var newAction = new UpdateAction ( UpdateType.CoinRemoved, data.Symbol );
Utility.UpdateActions.Enqueue ( newAction );
Console.WriteLine ( "{0} : Removed from buy logs", data.Symbol );
}
}
}
}