This is a synchronous solution I came up with that seems to work. Im wondering if I'm doing it sensibly and using it as it was intended to be used? Any comments from experienced users?
public List<Instrument> Search(IInstrumentProvider provider, InstrumentType type, string exchange, string symbol, byte currencyId) { Console.WriteLine($"Search({provider}, {type}, {exchange} ,{symbol} ,{currencyId})"); List<Instrument> found = new List<Instrument>(); string requestId = "${provider}.{type}.{exchange}.{symbol}.{currencyId}"; InstrumentDefinitionRequest definitionRequest = new InstrumentDefinitionRequest() { FilterExchange = exchange, FilterSymbol = symbol, FilterType = type, Id = requestId }; bool spin = true;
void OnInstrumentDefinition(object sender, InstrumentDefinitionEventArgs args) { Console.WriteLine($"OnInstrumentDefinition({args.Definition})"); if (args.Definition.RequestId == requestId) { foreach (Instrument instrument in args.Definition.Instruments) { if (instrument.CurrencyId == currencyId) { found.Add(instrument); } } Framework.EventManager.Dispatcher.InstrumentDefinition -= OnInstrumentDefinition; spin = false; } }
Framework.EventManager.Dispatcher.InstrumentDefinition += OnInstrumentDefinition; if (!provider.IsConnected) { provider.Connect(); } provider.Send(definitionRequest);
while (spin) { } return found; }
|