Andrew Harkins

Andrew Harkins

  • NA
  • 21
  • 777

C# Method being called multiple times but locking it for only 1 execut

Aug 16 2020 8:10 PM
Hello every1,
 
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. 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.
  1. public partial class MainWindow : Window  
  2. {  
  3. static readonly object lockIt = new object();  
  4. private void TurnOnSocket(){  
  5. var incoming = SocketClient.SubscribeToTradeUpdatesAsync("BTCUSD",(data) =>  
  6. {  
  7. if ((tExecute == null) || !tExecute.Status.Equals(TaskStatus.Running))  
  8. {  
  9. ExecuteTradeDelay();  
  10. //lock (lockIt)  
  11. //{  
  12. // // if(counterExecute == 0)  
  13. // // {  
  14. // // counterExecute++;  
  15. // ExecuteTradeAsync();  
  16. // // }  
  17. // //}  
  18. //}  
  19. _ = AddNewIncomingTradeAsync(data);  
  20. //counterExecute = 0;  
  21. // AddNewTrade(data);  
  22. // UpdateLstIncoming();  
  23. // tradingClient.AddNewTrade(data);  
  24. }  
  25. }  
  26. }  
  27. private async Task ExecuteTradeDelay()  
  28. {  
  29. await Task.Delay(1500);  
  30. lock (lockIt)  
  31. {  
  32. if(counterExecute == 0)  
  33. {  
  34. counterExecute++;  
  35. _ = ExecuteTradeAsync();  
  36. }  
  37. }  
  38. await Task.Delay(2500);  
  39. counterExecute = 0;  
  40. }  
  41. private async Task ExecuteTradeAsync()  
  42. {  
  43. cancelExecute = new CancellationTokenSource();  
  44. //if (counterExecute == 0) //{  
  45. //counterExecute++;  
  46. if (running)  
  47. //lock (lockSafe) //{  
  48. tExecute = Task.Factory.StartNew(async () =>  
  49. {  
  50. try  
  51. {  
  52. if (!did1B)  
  53. {  
  54. if (ActiveTrades.ContainsKey("Trade1B"))  
  55. {  
  56. if(UnsureWhyPriceValid(Convert.ToDecimal(CurrentPrice.Price), ActiveTrades["Trade1B"].StopPrice))  
  57. {  
  58. StopOrder stopOrder = new StopOrder(ActiveTrades["Trade1B"].TradeSymbol, ActiveTrades["Trade1B"].StopPrice, ActiveTrades["Trade1B"].LimitPrice, ActiveTrades["Trade1B"].CryptoAmount);  
  59. var placedOrder = await tradingClient.StopOrderBuyAsyncV2(stopOrder).ConfigureAwait(false);  
  60. AddTradeStatusList(placedOrder, "Trade1B");  
  61. HarkinsLogger.Log("Trade 1 - Buy Executed & added to 'TradeStatusList'");  
  62. trade1B = false;  
  63. ActiveTrades.Remove("Trade1B");  
  64. _ = PrintActiveTradesAsync();  
  65. return;  
  66. }  
  67. }  
  68. }  
  69. }  
  70. }