Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have some code that is connected to a websocket. And it can execute less than .0005 ms so it can be called basically instantly multiple times. I can only let this code execute once at a time. After the code finishes executing it can then execute again.

C#
public partial class MainWindow : Window
    {
static readonly object lockIt = new object();
private void TurnOnSocket(){
var incoming = SocketClient.SubscribeToTradeUpdatesAsync("BTCUSD",(data) =>
            {
if ((tExecute == null) || !tExecute.Status.Equals(TaskStatus.Running))
                {
                    ExecuteTradeDelay();
                    //lock (lockIt)
                    //{
                    //    //    if(counterExecute == 0)
                    //    //    {
                    //    //        counterExecute++;
                    //    ExecuteTradeAsync();
                    //    //    }                      
                    //    //}
                    //}

                    _ = AddNewIncomingTradeAsync(data);
                    //counterExecute = 0;
                    // AddNewTrade(data);
                    // UpdateLstIncoming();                   
                    // tradingClient.AddNewTrade(data); 
                }
          }
}
private async Task ExecuteTradeDelay()
        {
            await Task.Delay(1500);
            lock (lockIt)
            {
                if(counterExecute == 0)
                {
                    counterExecute++;
                    _ = ExecuteTradeAsync();
                }
            }
            await Task.Delay(2500);
            counterExecute = 0;
        }
        private async Task ExecuteTradeAsync()
        {
            cancelExecute = new CancellationTokenSource();
            //if (counterExecute == 0) //{
                //counterExecute++;
                if (running)
                {                    //lock (lockSafe) //{
                        tExecute = Task.Factory.StartNew(async () =>
                        {
                            try
                            {
                                if (!did1B)
                                {
                                    if (ActiveTrades.ContainsKey("Trade1B"))
                                    {
                                        if(UnsureWhyPriceValid(Convert.ToDecimal(CurrentPrice.Price), ActiveTrades["Trade1B"].StopPrice))
                                        {
                                            StopOrder stopOrder = new StopOrder(ActiveTrades["Trade1B"].TradeSymbol, ActiveTrades["Trade1B"].StopPrice, ActiveTrades["Trade1B"].LimitPrice, ActiveTrades["Trade1B"].CryptoAmount);
                                            var placedOrder = await tradingClient.StopOrderBuyAsyncV2(stopOrder).ConfigureAwait(false);
                                            AddTradeStatusList(placedOrder, "Trade1B");
                                            HarkinsLogger.Log("Trade 1 - Buy Executed & added to 'TradeStatusList'");
                                            trade1B = false;
                                            ActiveTrades.Remove("Trade1B");

                                            _ = PrintActiveTradesAsync();
                                            return;
                                        }
                                       
                                    }
                                }
           }
}


What I have tried:

I have tried using a lock statement, using a Counter, and switching many different parts of code around to make this happen properly. I got the code functioning how I want it to, or at least I think I do.. its working for now, I created a TradeDelay method but I know it this code is not setup properly and I would to learn proper implementation. I believe I should be able to completely remove this TradeDelay() and my ‘Counter’ once I have everything setup correctly.
Posted
Updated 16-Aug-20 17:17pm
Comments
[no name] 16-Aug-20 22:22pm    
If it works, it works. A (quick) "Q&A" is not about discussing abstracts.

1 solution

Quote:
I have some code that is connected to a websocket. And it can execute less than .0005 ms so it can be called basically instantly multiple times.
I would be looking at decoupling the receive vs processing logic - receive the data, queue it, done .. Another thread processes the items off the queue - that way you're not too concerned about delays, blocking your WebSocket etc
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900