private void Loop(CancellationToken token)
        {
            new Thread(() =>
            {
                while (!token.IsCancellationRequested)
                {
                    try
                    {
                        var tick = queue.DequeueAsync(token).Result;
                        logger.LogDebug("receive quote: {0}", tick);

                        quotes.AsParallel().Where(q => q.Source == tick.Symbol).ForAll(q =>
                        {
                            q.SetQuote(tick, logger);
                            if (q.Change)
                            {
                                var quoteString = q.ToUniFeederStringFormat();
                                logger.LogDebug("receive ({0} {1} {2}) => ({3} {4} {5}) use translate: {6}", tick.Symbol, tick.Bid, tick.Ask, q.Symbol, q.LastBid, q.LastAsk, q.ToStringTranslates());
                                var quoteUniFeederFormat = quoteString.ToUniFeederByteArray();
                                clients.AsParallel().ForAll(c =>
                                {
                                    try
                                    {
                                        c.Value.Send(quoteUniFeederFormat);
                                    }
                                    catch (Exception e)
                                    {
                                        logger.LogError("client: {0} error send quote: {1}", c.Key, e.Message);
                                        Task.Run(() => RemoveClient(c.Key, 5));
                                    }
                                });
                                logger.LogDebug("quote sending {0} clients", clients.Count);
                            }
                            else
                            {
                                logger.LogDebug("quote not changed therefore it is not sent by the client");
                            }
                        });
                    }
                    catch (Exception e)
                    {
                        if (e is OperationCanceledException || e.InnerException is OperationCanceledException)
                        {
                            break;
                        }
                        else
                        {
                            logger.LogError("error in queue processing: {0}", e.Message);
                        }
                    }
                }
            })
            {
                IsBackground = true
            }.Start();
        }
Example #2
0
        protected override async Task ExecuteAsync(CancellationToken token)
        {
            logger.LogInformation("Tws Process starting...");
            new Thread(() =>
            {
                while (!token.IsCancellationRequested)
                {
                    if (option.CurrentValue.Enable)
                    {
                        AutoProcessWindowTws(option.CurrentValue);
                    }
                }
            })
            {
                IsBackground = true
            }.Start();
            new Thread(async() =>
            {
                while (!token.IsCancellationRequested)
                {
                    if (option.CurrentValue.Enable)
                    {
                        try
                        {
                            var message_error = await state.DequeueAsync(token);
                            twsErrorMessageCount++;
                            logger.LogInformation("tws error count {0}, message:  {1}", twsErrorMessageCount, message_error);

                            if (twsErrorMessageCount >= option.CurrentValue.CriticalErrorMessageBeforeRestart)
                            {
                                if (RestartTwsProcess())
                                {
                                    twsErrorMessageCount = 0;
                                }
                            }
                        }
                        catch
                        { }
                    }
                }
            })
            {
                IsBackground = true
            }.Start();
            await Task.CompletedTask;

            logger.LogInformation("Tws Process started");
        }