Mercurial > pub > ImplabNet
annotate Implab.Diagnostics.Interactive/InteractiveListener.cs @ 242:cbe10ac0731e v3
Working on promises
| author | cin |
|---|---|
| date | Wed, 24 Jan 2018 03:03:21 +0300 |
| parents | eedf4d834e67 |
| children |
| 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; |
| 47 | 20 |
| 239 | 21 readonly SimpleAsyncQueue<TraceViewItem> m_queue = new SimpleAsyncQueue<TraceViewItem>(); |
| 47 | 22 readonly AutoResetEvent m_queueEvent = new AutoResetEvent(false); |
| 23 | |
| 24 int m_queueLength; | |
| 25 bool m_exitPending; | |
| 26 | |
| 27 readonly object m_pauseLock = new object(); | |
| 28 bool m_paused; | |
| 29 readonly ManualResetEvent m_pauseEvent = new ManualResetEvent(true); | |
| 30 | |
| 134 | 31 public InteractiveListener() { |
|
215
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
214
diff
changeset
|
32 m_guiFinished = RunGuiThread(); |
|
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
214
diff
changeset
|
33 AsyncPool.RunThread(QueueThread); |
| 47 | 34 |
| 35 m_guiStarted.Join(); | |
| 36 } | |
| 37 | |
| 38 void GuiThread() { | |
| 39 m_form = new TraceForm(); // will create SynchronizationContext | |
| 48 | 40 |
| 41 m_form.PauseEvents += (s,a) => Pause(); | |
| 42 m_form.ResumeEvents += (s, a) => Resume(); | |
| 43 | |
| 47 | 44 m_syncGuiThread = SynchronizationContext.Current; |
| 45 m_guiStarted.Resolve(); | |
| 46 Application.Run(); | |
| 47 } | |
| 48 | |
| 49 void QueueThread() { | |
| 50 while (!m_exitPending) { | |
| 51 if (m_paused) | |
| 52 m_pauseEvent.WaitOne(); | |
| 53 | |
| 54 TraceViewItem item; | |
| 55 if (m_queue.TryDequeue(out item)) { | |
| 56 Interlocked.Decrement(ref m_queueLength); | |
| 57 | |
|
214
9c32ef39b851
Added the time delta column to the interactive trace listener
cin
parents:
204
diff
changeset
|
58 m_syncGuiThread.Post(x => m_form.AddTraceEvent(item),null); |
| 47 | 59 } else { |
| 60 m_queueEvent.WaitOne(); | |
| 61 } | |
| 62 } | |
| 63 } | |
| 64 | |
|
215
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
214
diff
changeset
|
65 public IPromise RunGuiThread() { |
|
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
214
diff
changeset
|
66 var p = new Promise(); |
|
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
214
diff
changeset
|
67 |
|
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
214
diff
changeset
|
68 var caller = TraceContext.Instance.CurrentOperation; |
|
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
214
diff
changeset
|
69 |
|
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
214
diff
changeset
|
70 var worker = new Thread(() => { |
|
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
214
diff
changeset
|
71 TraceContext.Instance.EnterLogicalOperation(caller, false); |
|
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
214
diff
changeset
|
72 try { |
|
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
214
diff
changeset
|
73 Application.OleRequired(); |
|
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
214
diff
changeset
|
74 GuiThread(); |
|
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
214
diff
changeset
|
75 p.Resolve(); |
|
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
214
diff
changeset
|
76 } catch (Exception e) { |
|
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
214
diff
changeset
|
77 p.Reject(e); |
|
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
214
diff
changeset
|
78 } finally { |
|
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
214
diff
changeset
|
79 TraceContext.Instance.Leave(); |
|
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
214
diff
changeset
|
80 } |
|
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
214
diff
changeset
|
81 }); |
|
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
214
diff
changeset
|
82 worker.SetApartmentState(ApartmentState.STA); |
|
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
214
diff
changeset
|
83 worker.IsBackground = true; |
|
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
214
diff
changeset
|
84 worker.Name = string.Format("{0} GUI Thread", nameof(InteractiveListener)); |
|
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
214
diff
changeset
|
85 worker.Start(); |
|
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
214
diff
changeset
|
86 |
|
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
214
diff
changeset
|
87 return p; |
|
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
214
diff
changeset
|
88 } |
|
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
214
diff
changeset
|
89 |
| 47 | 90 public void Pause() { |
| 91 // for consistency we need to set this properties atomically | |
| 92 lock (m_pauseLock) { | |
| 93 m_pauseEvent.Reset(); | |
| 94 m_paused = true; | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 public void Resume() { | |
| 99 // for consistency we need to set this properties atomically | |
| 100 lock (m_pauseLock) { | |
| 101 m_paused = false; | |
| 102 m_pauseEvent.Set(); | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 void Enqueue(TraceViewItem item) { | |
| 107 m_queue.Enqueue(item); | |
| 108 if (Interlocked.Increment(ref m_queueLength) == 1) | |
| 109 m_queueEvent.Set(); | |
| 110 } | |
| 111 | |
| 112 public void ShowForm() { | |
| 113 m_syncGuiThread.Post(x => m_form.Show(), null); | |
| 114 } | |
| 115 | |
| 116 public void HideForm() { | |
| 117 m_syncGuiThread.Post(x => m_form.Hide(), null); | |
| 118 } | |
| 119 | |
| 120 void Terminate() { | |
| 48 | 121 m_exitPending = true; |
| 122 Resume(); | |
| 47 | 123 m_syncGuiThread.Post(x => Application.ExitThread(), null); |
| 124 } | |
| 125 | |
| 126 protected override void Dispose(bool disposing) { | |
| 127 if (disposing) { | |
| 128 Terminate(); | |
| 129 m_guiFinished.Join(); | |
| 130 } | |
| 131 base.Dispose(disposing); | |
| 132 } | |
| 48 | 133 |
| 134 | 134 public override void Write(LogEventArgs args, object entry) { |
| 48 | 135 var item = new TraceViewItem { |
| 134 | 136 Indent = args.Operation.Level, |
| 137 Message = entry.ToString(), | |
| 92 | 138 Thread = args.ThreadId, |
| 204 | 139 Channel = args.Channel.ToString(), |
|
214
9c32ef39b851
Added the time delta column to the interactive trace listener
cin
parents:
204
diff
changeset
|
140 Timestamp = Environment.TickCount, |
|
9c32ef39b851
Added the time delta column to the interactive trace listener
cin
parents:
204
diff
changeset
|
141 TimeDelta = args.OperationTimeOffset |
| 48 | 142 }; |
| 143 | |
| 144 Enqueue(item); | |
| 145 } | |
| 47 | 146 } |
| 147 } |
