Пример #1
0
        private static void MessageThread(MLLPServer server, MeasurementSequence messageSequence, int delay, CancellationToken token, Action <Measurement> onSend)
        {
            while (!token.IsCancellationRequested)
            {
                var measurement = messageSequence.GetMeasurement();

                if (measurement != null)
                {
                    var message = BuildHL7Message(measurement.Value);

                    List <Task> tasks = new List <Task>();

                    foreach (MLLPClient client in server.Clients())
                    {
                        tasks.Add(client.SendMessageAsync(message));
                    }

                    onSend?.Invoke(measurement.Value);

                    try
                    {
                        Task.WaitAll(tasks.ToArray(), token);
                    }
                    catch {}

                    if (token.IsCancellationRequested)
                    {
                        break;
                    }
                }

                token.WaitHandle.WaitOne(delay);
            }
        }
Пример #2
0
        public void StartServer(int port)
        {
            if (_server == null || _server.Port != port)
            {
                _server = new MLLPServer(port);
            }

            _server.Start();

            _cts            = new CancellationTokenSource();
            _messagesThread = new Thread(() => MessageThread(_server, _messageSequence, _delay, _cts.Token, OnSend));
            _messagesThread.Start();
        }