annotate Implab.Diagnostics.Interactive/InteractiveListener.cs @ 239:eedf4d834e67 v2

fix
author cin
date Wed, 13 Dec 2017 19:54:45 +0300
parents fe5101083150
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
47
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
1 using Implab.Parallels;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
2 using System;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
3 using System.Collections.Generic;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
4 using System.Linq;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
5 using System.Text;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
6 using System.Threading;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
7 using System.Threading.Tasks;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
8 using System.Windows.Forms;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
9
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
10 namespace Implab.Diagnostics.Interactive
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
11 {
134
04d4c92d0f28 Improved logging
cin
parents: 130
diff changeset
12 public class InteractiveListener: ListenerBase
47
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
13 {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
14 TraceForm m_form;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
15
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
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
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
18
66
790e8a997d30 Refactoring
cin
parents: 48
diff changeset
19 readonly IPromise m_guiFinished;
47
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
20
239
cin
parents: 215
diff changeset
21 readonly SimpleAsyncQueue<TraceViewItem> m_queue = new SimpleAsyncQueue<TraceViewItem>();
47
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
22 readonly AutoResetEvent m_queueEvent = new AutoResetEvent(false);
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
23
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
24 int m_queueLength;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
25 bool m_exitPending;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
26
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
27 readonly object m_pauseLock = new object();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
28 bool m_paused;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
29 readonly ManualResetEvent m_pauseEvent = new ManualResetEvent(true);
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
30
134
04d4c92d0f28 Improved logging
cin
parents: 130
diff changeset
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
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
34
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
35 m_guiStarted.Join();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
36 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
37
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
38 void GuiThread() {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
39 m_form = new TraceForm(); // will create SynchronizationContext
48
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
40
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
41 m_form.PauseEvents += (s,a) => Pause();
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
42 m_form.ResumeEvents += (s, a) => Resume();
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
43
47
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
44 m_syncGuiThread = SynchronizationContext.Current;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
45 m_guiStarted.Resolve();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
46 Application.Run();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
47 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
48
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
49 void QueueThread() {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
50 while (!m_exitPending) {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
51 if (m_paused)
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
52 m_pauseEvent.WaitOne();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
53
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
54 TraceViewItem item;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
55 if (m_queue.TryDequeue(out item)) {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
56 Interlocked.Decrement(ref m_queueLength);
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
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
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
59 } else {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
60 m_queueEvent.WaitOne();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
61 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
62 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
63 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
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
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
90 public void Pause() {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
91 // for consistency we need to set this properties atomically
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
92 lock (m_pauseLock) {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
93 m_pauseEvent.Reset();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
94 m_paused = true;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
95 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
96 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
97
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
98 public void Resume() {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
99 // for consistency we need to set this properties atomically
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
100 lock (m_pauseLock) {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
101 m_paused = false;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
102 m_pauseEvent.Set();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
103 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
104 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
105
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
106 void Enqueue(TraceViewItem item) {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
107 m_queue.Enqueue(item);
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
108 if (Interlocked.Increment(ref m_queueLength) == 1)
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
109 m_queueEvent.Set();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
110 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
111
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
112 public void ShowForm() {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
113 m_syncGuiThread.Post(x => m_form.Show(), null);
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
114 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
115
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
116 public void HideForm() {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
117 m_syncGuiThread.Post(x => m_form.Hide(), null);
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
118 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
119
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
120 void Terminate() {
48
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
121 m_exitPending = true;
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
122 Resume();
47
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
123 m_syncGuiThread.Post(x => Application.ExitThread(), null);
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
124 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
125
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
126 protected override void Dispose(bool disposing) {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
127 if (disposing) {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
128 Terminate();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
129 m_guiFinished.Join();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
130 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
131 base.Dispose(disposing);
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
132 }
48
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
133
134
04d4c92d0f28 Improved logging
cin
parents: 130
diff changeset
134 public override void Write(LogEventArgs args, object entry) {
48
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
135 var item = new TraceViewItem {
134
04d4c92d0f28 Improved logging
cin
parents: 130
diff changeset
136 Indent = args.Operation.Level,
04d4c92d0f28 Improved logging
cin
parents: 130
diff changeset
137 Message = entry.ToString(),
92
4c0e5ef99986 rewritten tracing
cin
parents: 85
diff changeset
138 Thread = args.ThreadId,
204
cbb0bd8fc0d1 Fixed broken Implab.Diagnostics.Interactive
cin
parents: 134
diff changeset
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
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
142 };
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
143
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
144 Enqueue(item);
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
145 }
47
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
146 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
147 }