public void Enqueue(string text) { if (state == RUNNING) { Queue.Enqueue(text); Enqueued.Set(); } else { Console.WriteLine("{0}", text); OnPutLine?.Invoke(text); } }
private void MainLoop() { try { Interlocked.CompareExchange(ref state, RUNNING, STARTUP_PENDING); OnStartUp?.Invoke(); while (true) { if (Queue.TryDequeue(out var text)) { Console.WriteLine("{0}", text); OnPutLine?.Invoke(text); } else if (state == RUNNING) { Enqueued.WaitOne(); } else { break; } } Interlocked.Exchange(ref state, SHUTDOWN); OnShutDown?.Invoke(); } catch (Exception e) { Interlocked.Exchange(ref state, ERROR); Console.Error.WriteLine("ERROR: {0}", e.Message); while ((e = e.InnerException) != null) { Console.Error.WriteLine(" {0}", e.Message); } OnError?.Invoke(e); } }