comparison Implab.Diagnostics.Interactive/InteractiveListener.cs @ 48:d9d794b61bb9 interactive logger

Interactive tracing Improved working with tracing contexts
author cin
date Fri, 18 Apr 2014 12:34:45 +0400
parents b181f7bcb259
children 790e8a997d30
comparison
equal deleted inserted replaced
47:b181f7bcb259 48:d9d794b61bb9
7 using System.Threading.Tasks; 7 using System.Threading.Tasks;
8 using System.Windows.Forms; 8 using System.Windows.Forms;
9 9
10 namespace Implab.Diagnostics.Interactive 10 namespace Implab.Diagnostics.Interactive
11 { 11 {
12 public class InteractiveListener: Disposable 12 public class InteractiveListener: TextListenerBase
13 { 13 {
14 TraceForm m_form; 14 TraceForm m_form;
15 15
16 SynchronizationContext m_syncGuiThread; 16 SynchronizationContext m_syncGuiThread;
17 readonly Promise<object> m_guiStarted = new Promise<object>();
17 18
18 readonly IPromiseBase m_guiFinished; 19 readonly IPromiseBase m_guiFinished;
19 readonly Promise<object> m_guiStarted = new Promise<object>();
20
21 readonly IPromiseBase m_workerFinished = new Promise<object>(); 20 readonly IPromiseBase m_workerFinished = new Promise<object>();
22 21
23 readonly MTQueue<TraceViewItem> m_queue = new MTQueue<TraceViewItem>(); 22 readonly MTQueue<TraceViewItem> m_queue = new MTQueue<TraceViewItem>();
24 readonly AutoResetEvent m_queueEvent = new AutoResetEvent(false); 23 readonly AutoResetEvent m_queueEvent = new AutoResetEvent(false);
25 24
28 27
29 readonly object m_pauseLock = new object(); 28 readonly object m_pauseLock = new object();
30 bool m_paused; 29 bool m_paused;
31 readonly ManualResetEvent m_pauseEvent = new ManualResetEvent(true); 30 readonly ManualResetEvent m_pauseEvent = new ManualResetEvent(true);
32 31
33 public InteractiveListener() { 32 public InteractiveListener(bool global) : base(global) {
34 m_guiFinished = AsyncPool.InvokeNewThread(() => { 33 m_guiFinished = AsyncPool.InvokeNewThread(GuiThread);
35 GuiThread(); 34 m_workerFinished = AsyncPool.InvokeNewThread(QueueThread);
36 return 0;
37 });
38 35
39 m_guiStarted.Join(); 36 m_guiStarted.Join();
40 } 37 }
41 38
42 void GuiThread() { 39 void GuiThread() {
43 m_form = new TraceForm(); // will create SynchronizationContext 40 m_form = new TraceForm(); // will create SynchronizationContext
41
42 m_form.PauseEvents += (s,a) => Pause();
43 m_form.ResumeEvents += (s, a) => Resume();
44
44 m_syncGuiThread = SynchronizationContext.Current; 45 m_syncGuiThread = SynchronizationContext.Current;
45 m_guiStarted.Resolve(); 46 m_guiStarted.Resolve();
46 Application.Run(); 47 Application.Run();
47 } 48 }
48 49
91 public void HideForm() { 92 public void HideForm() {
92 m_syncGuiThread.Post(x => m_form.Hide(), null); 93 m_syncGuiThread.Post(x => m_form.Hide(), null);
93 } 94 }
94 95
95 void Terminate() { 96 void Terminate() {
97 m_exitPending = true;
98 Resume();
96 m_syncGuiThread.Post(x => Application.ExitThread(), null); 99 m_syncGuiThread.Post(x => Application.ExitThread(), null);
97 } 100 }
98 101
99 protected override void Dispose(bool disposing) { 102 protected override void Dispose(bool disposing) {
100 if (disposing) { 103 if (disposing) {
101 Terminate(); 104 Terminate();
102 m_guiFinished.Join(); 105 m_guiFinished.Join();
103 } 106 }
104 base.Dispose(disposing); 107 base.Dispose(disposing);
105 } 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 }
106 } 121 }
107 } 122 }