mirror of
https://github.com/vdemydiuk/mtapi.git
synced 2026-07-27 18:47:55 +00:00
Issue #296: MtApi5 - Added property CommandTimeout to define time for waiting result of response from MetaTrader. Default value 30 sec
This commit is contained in:
+14
-1
@@ -32,11 +32,24 @@ namespace MtApi5
|
|||||||
|
|
||||||
private HashSet<int> _experts = [];
|
private HashSet<int> _experts = [];
|
||||||
private Dictionary<int, Mt5Quote> _quotes = [];
|
private Dictionary<int, Mt5Quote> _quotes = [];
|
||||||
|
private volatile int _command_timeout = 30000; // 30 seconds
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Public Methods
|
#region Public Methods
|
||||||
private IMtLogger Log { get; }
|
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)
|
public MtApi5Client(IMtLogger? log = null)
|
||||||
{
|
{
|
||||||
_mtEventHandlers[Mt5EventTypes.OnBookEvent] = ReceivedOnBookEvent;
|
_mtEventHandlers[Mt5EventTypes.OnBookEvent] = ReceivedOnBookEvent;
|
||||||
@@ -3588,7 +3601,7 @@ namespace MtApi5
|
|||||||
var payloadJson = payload == null ? string.Empty : JsonConvert.SerializeObject(payload);
|
var payloadJson = payload == null ? string.Empty : JsonConvert.SerializeObject(payload);
|
||||||
Log.Debug($"SendCommand: sending '{payloadJson}' ...");
|
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}]");
|
Log.Debug($"SendCommand: received response JSON [{responseJson}]");
|
||||||
|
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ namespace MtClient
|
|||||||
logger_.Debug($"MtRpcClient.Disconnect: success");
|
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<string> commandTask = new();
|
CommandTask<string> commandTask = new();
|
||||||
int commandId;
|
int commandId;
|
||||||
@@ -78,7 +78,7 @@ namespace MtClient
|
|||||||
MtCommand command = new(expertHandle, commandType, commandId, payload);
|
MtCommand command = new(expertHandle, commandType, commandId, payload);
|
||||||
Send(command);
|
Send(command);
|
||||||
|
|
||||||
var response = commandTask.WaitResponse(10000); // 10 sec
|
var response = commandTask.WaitResponse(timeout);
|
||||||
lock (tasks_)
|
lock (tasks_)
|
||||||
{
|
{
|
||||||
tasks_.Remove(commandId);
|
tasks_.Remove(commandId);
|
||||||
|
|||||||
@@ -167,6 +167,7 @@
|
|||||||
<RowDefinition Height="Auto"/>
|
<RowDefinition Height="Auto"/>
|
||||||
<RowDefinition Height="*"/>
|
<RowDefinition Height="*"/>
|
||||||
<RowDefinition Height="Auto"/>
|
<RowDefinition Height="Auto"/>
|
||||||
|
<RowDefinition Height="Auto"/>
|
||||||
</Grid.RowDefinitions>
|
</Grid.RowDefinitions>
|
||||||
<Label Grid.Row="0" Content="Quotes" Background="LightYellow" />
|
<Label Grid.Row="0" Content="Quotes" Background="LightYellow" />
|
||||||
<ListView Grid.Row="1" Margin="2"
|
<ListView Grid.Row="1" Margin="2"
|
||||||
@@ -183,6 +184,11 @@
|
|||||||
</ListView.View>
|
</ListView.View>
|
||||||
</ListView>
|
</ListView>
|
||||||
<Button Grid.Row="2" Content="Refresh Quotes" Margin="2" Command="{Binding RefreshQuotesCommand}"/>
|
<Button Grid.Row="2" Content="Refresh Quotes" Margin="2" Command="{Binding RefreshQuotesCommand}"/>
|
||||||
|
<StackPanel Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Left" Margin="2">
|
||||||
|
<TextBlock Text="Command timeout (ms):" VerticalAlignment="Center" Margin="2"/>
|
||||||
|
<TextBox Text="{Binding CommandTimeout}" Width="100" Margin="2"/>
|
||||||
|
<Button Content="Apply" Margin="2" Command="{Binding TimeoutApplyCommand}"/>
|
||||||
|
</StackPanel>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Border>
|
</Border>
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|||||||
@@ -163,6 +163,8 @@ namespace MtApi5TestClient
|
|||||||
|
|
||||||
public DelegateCommand GetSymbolsCommand { get; private set; }
|
public DelegateCommand GetSymbolsCommand { get; private set; }
|
||||||
public DelegateCommand RefreshQuotesCommand { get; private set; }
|
public DelegateCommand RefreshQuotesCommand { get; private set; }
|
||||||
|
|
||||||
|
public DelegateCommand TimeoutApplyCommand { get; private set; }
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
@@ -210,6 +212,17 @@ namespace MtApi5TestClient
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int _command_timeout;
|
||||||
|
public int CommandTimeout
|
||||||
|
{
|
||||||
|
get { return _command_timeout; }
|
||||||
|
set
|
||||||
|
{
|
||||||
|
_command_timeout = value;
|
||||||
|
OnPropertyChanged("CommandTimeout");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public ObservableCollection<QuoteViewModel> Quotes { get; } = new ObservableCollection<QuoteViewModel>();
|
public ObservableCollection<QuoteViewModel> Quotes { get; } = new ObservableCollection<QuoteViewModel>();
|
||||||
|
|
||||||
private QuoteViewModel _selectedQuote;
|
private QuoteViewModel _selectedQuote;
|
||||||
@@ -350,6 +363,7 @@ namespace MtApi5TestClient
|
|||||||
ConnectionState = _mtApiClient.ConnectionState;
|
ConnectionState = _mtApiClient.ConnectionState;
|
||||||
ConnectionMessage = "Disconnected";
|
ConnectionMessage = "Disconnected";
|
||||||
Port = 8228; //default local port
|
Port = 8228; //default local port
|
||||||
|
CommandTimeout = _mtApiClient.CommandTimeout;
|
||||||
|
|
||||||
InitCommands();
|
InitCommands();
|
||||||
|
|
||||||
@@ -494,6 +508,8 @@ namespace MtApi5TestClient
|
|||||||
|
|
||||||
GetSymbolsCommand = new DelegateCommand(ExecuteGetSymbols);
|
GetSymbolsCommand = new DelegateCommand(ExecuteGetSymbols);
|
||||||
RefreshQuotesCommand = new DelegateCommand(ExecuteRefreshQuotes);
|
RefreshQuotesCommand = new DelegateCommand(ExecuteRefreshQuotes);
|
||||||
|
|
||||||
|
TimeoutApplyCommand = new DelegateCommand(ExecuteTimeoutApply);
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool CanExecuteConnect(object o)
|
private bool CanExecuteConnect(object o)
|
||||||
@@ -1773,6 +1789,12 @@ namespace MtApi5TestClient
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ExecuteTimeoutApply(object o)
|
||||||
|
{
|
||||||
|
_mtApiClient.CommandTimeout = CommandTimeout;
|
||||||
|
AddLog($"TimeoutApply: timeout = {CommandTimeout} milliseconds");
|
||||||
|
}
|
||||||
|
|
||||||
private static void RunOnUiThread(Action action)
|
private static void RunOnUiThread(Action action)
|
||||||
{
|
{
|
||||||
Application.Current?.Dispatcher.Invoke(action);
|
Application.Current?.Dispatcher.Invoke(action);
|
||||||
|
|||||||
Reference in New Issue
Block a user