From fa1e6b6c62b1212b0bcf548ac2a99d86d6f4be0f Mon Sep 17 00:00:00 2001 From: Viacheslav Demydiuk Date: Mon, 6 Apr 2026 20:22:10 +0300 Subject: [PATCH 1/4] Issue #296: MtApi5 - Added property CommandTimeout to define time for waiting result of response from MetaTrader. Default value 30 sec --- MtApi5/MtApi5Client.cs | 15 ++++++++++++- MtClient/MtRpcClient.cs | 4 ++-- TestClients/MtApi5TestClient/MainWindow.xaml | 6 ++++++ TestClients/MtApi5TestClient/ViewModel.cs | 22 ++++++++++++++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/MtApi5/MtApi5Client.cs b/MtApi5/MtApi5Client.cs index a80ffd57..2ace4cbe 100755 --- a/MtApi5/MtApi5Client.cs +++ b/MtApi5/MtApi5Client.cs @@ -32,11 +32,24 @@ namespace MtApi5 private HashSet _experts = []; private Dictionary _quotes = []; + private volatile int _command_timeout = 30000; // 30 seconds #endregion #region Public Methods private IMtLogger Log { get; } + // Time in milliseconds to wait for a response from MetaTrader for a command. Default is 30 seconds. + public int CommandTimeout + { + get => _command_timeout; + set + { + if (value <= 0) + throw new ArgumentException("Command timeout must be greater than zero."); + _command_timeout = value; + } + } + public MtApi5Client(IMtLogger? log = null) { _mtEventHandlers[Mt5EventTypes.OnBookEvent] = ReceivedOnBookEvent; @@ -3588,7 +3601,7 @@ namespace MtApi5 var payloadJson = payload == null ? string.Empty : JsonConvert.SerializeObject(payload); Log.Debug($"SendCommand: sending '{payloadJson}' ..."); - var responseJson = client.SendCommand(expertHandle, (int)commandType, payloadJson); + var responseJson = client.SendCommand(expertHandle, (int)commandType, payloadJson, CommandTimeout); Log.Debug($"SendCommand: received response JSON [{responseJson}]"); diff --git a/MtClient/MtRpcClient.cs b/MtClient/MtRpcClient.cs index c1fbbeeb..a85aee34 100755 --- a/MtClient/MtRpcClient.cs +++ b/MtClient/MtRpcClient.cs @@ -65,7 +65,7 @@ namespace MtClient logger_.Debug($"MtRpcClient.Disconnect: success"); } - public string? SendCommand(int expertHandle, int commandType, string payload) + public string? SendCommand(int expertHandle, int commandType, string payload, int timeout = 10000) // 10 sec { CommandTask commandTask = new(); int commandId; @@ -78,7 +78,7 @@ namespace MtClient MtCommand command = new(expertHandle, commandType, commandId, payload); Send(command); - var response = commandTask.WaitResponse(10000); // 10 sec + var response = commandTask.WaitResponse(timeout); lock (tasks_) { tasks_.Remove(commandId); diff --git a/TestClients/MtApi5TestClient/MainWindow.xaml b/TestClients/MtApi5TestClient/MainWindow.xaml index e257be3a..c30fe43e 100755 --- a/TestClients/MtApi5TestClient/MainWindow.xaml +++ b/TestClients/MtApi5TestClient/MainWindow.xaml @@ -167,6 +167,7 @@ +