public EventLoopMessageListener(Action <T> processMessage, string threadName = null)
        {
#if DEBUG
            _CreatorStackTrace = new StackTrace(true);
#endif

            thread = new Thread(new ThreadStart(() =>
            {
                try
                {
                    while (!cancellationSource.IsCancellationRequested)
                    {
                        Wrapper <T> message;

                        message = _MessageQueue.Take(cancellationSource.Token);

                        continueEvent.WaitOne();

                        if (message != null)
                        {
                            try
                            {
                                //InfrastructureTrace.Information("processMessage: " + message.GetType());
                                processMessage(message.Value);
                            }
                            catch (Exception ex)
                            {
                                InfrastructureTrace.Error("Exception during message loop", ex);
                                Console.WriteLine("Exception during message loop", ex);                                 //TODO: write to trace
                                //NodeServerTrace.Error("Exception during message loop", ex);

                                throw ex;
                            }
                        }
                    }
                }
                catch (OperationCanceledException)
                {
                }
            }));

            thread.Name = threadName ?? "EventLoopMessageHandler";
            thread.Start();
        }
Example #2
0
        private void Ensure()
        {
            lock (_sync)
            {
                if (_Value != null)
                {
                    return;
                }

                if (_FileName == null)
                {
                    throw new Exception("Missing file name for " + GetType());
                }

                if (File.Exists(_FileName))
                {
                    try
                    {
                        _Value   = JsonConvert.DeserializeObject <T>(File.ReadAllText(_FileName));
                        _Corrupt = false;
                    }
                    catch
                    {
                        InfrastructureTrace.Warning($"File corrupt: {_FileName}");
                        _Corrupt = true;
                    }
                }
            }

            if (_Value == null)
            {
                _IsNew = true;
                _Value = new T();
            }
            else
            {
                _IsNew = false;
            }
        }