Mercurial > pub > ImplabNet
comparison Implab.Diagnostics.Interactive/InteractiveListener.cs @ 50:f8cbe84cfdb1
Слияние с interactive logger
| author | cin |
|---|---|
| date | Fri, 18 Apr 2014 12:37:48 +0400 |
| parents | d9d794b61bb9 |
| children | 790e8a997d30 |
comparison
equal
deleted
inserted
replaced
| 44:e5ec543feee3 | 50:f8cbe84cfdb1 |
|---|---|
| 1 using Implab.Parallels; | |
| 2 using System; | |
| 3 using System.Collections.Generic; | |
| 4 using System.Linq; | |
| 5 using System.Text; | |
| 6 using System.Threading; | |
| 7 using System.Threading.Tasks; | |
| 8 using System.Windows.Forms; | |
| 9 | |
| 10 namespace Implab.Diagnostics.Interactive | |
| 11 { | |
| 12 public class InteractiveListener: TextListenerBase | |
| 13 { | |
| 14 TraceForm m_form; | |
| 15 | |
| 16 SynchronizationContext m_syncGuiThread; | |
| 17 readonly Promise<object> m_guiStarted = new Promise<object>(); | |
| 18 | |
| 19 readonly IPromiseBase m_guiFinished; | |
| 20 readonly IPromiseBase m_workerFinished = new Promise<object>(); | |
| 21 | |
| 22 readonly MTQueue<TraceViewItem> m_queue = new MTQueue<TraceViewItem>(); | |
| 23 readonly AutoResetEvent m_queueEvent = new AutoResetEvent(false); | |
| 24 | |
| 25 int m_queueLength; | |
| 26 bool m_exitPending; | |
| 27 | |
| 28 readonly object m_pauseLock = new object(); | |
| 29 bool m_paused; | |
| 30 readonly ManualResetEvent m_pauseEvent = new ManualResetEvent(true); | |
| 31 | |
| 32 public InteractiveListener(bool global) : base(global) { | |
| 33 m_guiFinished = AsyncPool.InvokeNewThread(GuiThread); | |
| 34 m_workerFinished = AsyncPool.InvokeNewThread(QueueThread); | |
| 35 | |
| 36 m_guiStarted.Join(); | |
| 37 } | |
| 38 | |
| 39 void GuiThread() { | |
| 40 m_form = new TraceForm(); // will create SynchronizationContext | |
| 41 | |
| 42 m_form.PauseEvents += (s,a) => Pause(); | |
| 43 m_form.ResumeEvents += (s, a) => Resume(); | |
| 44 | |
| 45 m_syncGuiThread = SynchronizationContext.Current; | |
| 46 m_guiStarted.Resolve(); | |
| 47 Application.Run(); | |
| 48 } | |
| 49 | |
| 50 void QueueThread() { | |
| 51 while (!m_exitPending) { | |
| 52 if (m_paused) | |
| 53 m_pauseEvent.WaitOne(); | |
| 54 | |
| 55 TraceViewItem item; | |
| 56 if (m_queue.TryDequeue(out item)) { | |
| 57 Interlocked.Decrement(ref m_queueLength); | |
| 58 | |
| 59 m_syncGuiThread.Send(x => m_form.AddTraceEvent(item),null); | |
| 60 } else { | |
| 61 m_queueEvent.WaitOne(); | |
| 62 } | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 public void Pause() { | |
| 67 // for consistency we need to set this properties atomically | |
| 68 lock (m_pauseLock) { | |
| 69 m_pauseEvent.Reset(); | |
| 70 m_paused = true; | |
| 71 } | |
| 72 } | |
| 73 | |
| 74 public void Resume() { | |
| 75 // for consistency we need to set this properties atomically | |
| 76 lock (m_pauseLock) { | |
| 77 m_paused = false; | |
| 78 m_pauseEvent.Set(); | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 void Enqueue(TraceViewItem item) { | |
| 83 m_queue.Enqueue(item); | |
| 84 if (Interlocked.Increment(ref m_queueLength) == 1) | |
| 85 m_queueEvent.Set(); | |
| 86 } | |
| 87 | |
| 88 public void ShowForm() { | |
| 89 m_syncGuiThread.Post(x => m_form.Show(), null); | |
| 90 } | |
| 91 | |
| 92 public void HideForm() { | |
| 93 m_syncGuiThread.Post(x => m_form.Hide(), null); | |
| 94 } | |
| 95 | |
| 96 void Terminate() { | |
| 97 m_exitPending = true; | |
| 98 Resume(); | |
| 99 m_syncGuiThread.Post(x => Application.ExitThread(), null); | |
| 100 } | |
| 101 | |
| 102 protected override void Dispose(bool disposing) { | |
| 103 if (disposing) { | |
| 104 Terminate(); | |
| 105 m_guiFinished.Join(); | |
| 106 } | |
| 107 base.Dispose(disposing); | |
| 108 } | |
| 109 | |
| 110 protected override void WriteEntry(TraceContext context, EventText text, string channel) { | |
| 111 var item = new TraceViewItem { | |
| 112 Indent = text.indent, | |
| 113 Message = text.content, | |
| 114 Thread = context.ThreadId, | |
| 115 Channel = channel, | |
| 116 Timestamp = Environment.TickCount | |
| 117 }; | |
| 118 | |
| 119 Enqueue(item); | |
| 120 } | |
| 121 } | |
| 122 } |
