Mercurial > pub > ImplabNet
annotate Implab.Diagnostics.Interactive/InteractiveListener.cs @ 144:8c0b95069066 v2
DRAFT: refactoring
author | cin |
---|---|
date | Fri, 06 Mar 2015 15:45:26 +0300 |
parents | 04d4c92d0f28 |
children | cbb0bd8fc0d1 |
rev | line source |
---|---|
47 | 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 { | |
134 | 12 public class InteractiveListener: ListenerBase |
47 | 13 { |
14 TraceForm m_form; | |
15 | |
16 SynchronizationContext m_syncGuiThread; | |
130
671f60cd0250
fixed Resove method bug when calling it on already cancelled promise
cin
parents:
92
diff
changeset
|
17 readonly Promise m_guiStarted = new Promise(); |
47 | 18 |
66 | 19 readonly IPromise m_guiFinished; |
85 | 20 // readonly IPromise m_workerFinished = new Promise<object>(); |
47 | 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 | |
134 | 32 public InteractiveListener() { |
130
671f60cd0250
fixed Resove method bug when calling it on already cancelled promise
cin
parents:
92
diff
changeset
|
33 m_guiFinished = AsyncPool.RunThread(GuiThread); |
671f60cd0250
fixed Resove method bug when calling it on already cancelled promise
cin
parents:
92
diff
changeset
|
34 /*m_workerFinished = */AsyncPool.RunThread(QueueThread); |
47 | 35 |
36 m_guiStarted.Join(); | |
37 } | |
38 | |
39 void GuiThread() { | |
40 m_form = new TraceForm(); // will create SynchronizationContext | |
48 | 41 |
42 m_form.PauseEvents += (s,a) => Pause(); | |
43 m_form.ResumeEvents += (s, a) => Resume(); | |
44 | |
47 | 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() { | |
48 | 97 m_exitPending = true; |
98 Resume(); | |
47 | 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 } | |
48 | 109 |
134 | 110 public override void Write(LogEventArgs args, object entry) { |
48 | 111 var item = new TraceViewItem { |
134 | 112 Indent = args.Operation.Level, |
113 Message = entry.ToString(), | |
92 | 114 Thread = args.ThreadId, |
134 | 115 Channel = args.ChannelName, |
48 | 116 Timestamp = Environment.TickCount |
117 }; | |
118 | |
119 Enqueue(item); | |
120 } | |
47 | 121 } |
122 } |