annotate Implab.Diagnostics.Interactive/InteractiveListener.cs @ 187:dd4a3590f9c6 ref20160224

Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler Any unhandled OperationCanceledException will cause the promise cancelation
author cin
date Tue, 19 Apr 2016 17:35:20 +0300
parents 04d4c92d0f28
children cbb0bd8fc0d1
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;
85
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 66
diff changeset
20 // readonly IPromise m_workerFinished = new Promise<object>();
47
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
21
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
22 readonly MTQueue<TraceViewItem> m_queue = new MTQueue<TraceViewItem>();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
23 readonly AutoResetEvent m_queueEvent = new AutoResetEvent(false);
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
24
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
25 int m_queueLength;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
26 bool m_exitPending;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
27
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
28 readonly object m_pauseLock = new object();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
29 bool m_paused;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
30 readonly ManualResetEvent m_pauseEvent = new ManualResetEvent(true);
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
31
134
04d4c92d0f28 Improved logging
cin
parents: 130
diff changeset
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
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
35
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
36 m_guiStarted.Join();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
37 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
38
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
39 void GuiThread() {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
40 m_form = new TraceForm(); // will create SynchronizationContext
48
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
41
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
42 m_form.PauseEvents += (s,a) => Pause();
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
43 m_form.ResumeEvents += (s, a) => Resume();
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
44
47
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
45 m_syncGuiThread = SynchronizationContext.Current;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
46 m_guiStarted.Resolve();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
47 Application.Run();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
48 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
49
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
50 void QueueThread() {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
51 while (!m_exitPending) {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
52 if (m_paused)
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
53 m_pauseEvent.WaitOne();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
54
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
55 TraceViewItem item;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
56 if (m_queue.TryDequeue(out item)) {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
57 Interlocked.Decrement(ref m_queueLength);
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
58
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
59 m_syncGuiThread.Send(x => m_form.AddTraceEvent(item),null);
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
60 } else {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
61 m_queueEvent.WaitOne();
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 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
65
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
66 public void Pause() {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
67 // for consistency we need to set this properties atomically
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
68 lock (m_pauseLock) {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
69 m_pauseEvent.Reset();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
70 m_paused = true;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
71 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
72 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
73
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
74 public void Resume() {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
75 // for consistency we need to set this properties atomically
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
76 lock (m_pauseLock) {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
77 m_paused = false;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
78 m_pauseEvent.Set();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
79 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
80 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
81
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
82 void Enqueue(TraceViewItem item) {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
83 m_queue.Enqueue(item);
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
84 if (Interlocked.Increment(ref m_queueLength) == 1)
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
85 m_queueEvent.Set();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
86 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
87
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
88 public void ShowForm() {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
89 m_syncGuiThread.Post(x => m_form.Show(), null);
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
90 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
91
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
92 public void HideForm() {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
93 m_syncGuiThread.Post(x => m_form.Hide(), null);
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
94 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
95
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
96 void Terminate() {
48
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
97 m_exitPending = true;
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
98 Resume();
47
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
99 m_syncGuiThread.Post(x => Application.ExitThread(), null);
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
100 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
101
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
102 protected override void Dispose(bool disposing) {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
103 if (disposing) {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
104 Terminate();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
105 m_guiFinished.Join();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
106 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
107 base.Dispose(disposing);
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
108 }
48
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
109
134
04d4c92d0f28 Improved logging
cin
parents: 130
diff changeset
110 public override void Write(LogEventArgs args, object entry) {
48
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
111 var item = new TraceViewItem {
134
04d4c92d0f28 Improved logging
cin
parents: 130
diff changeset
112 Indent = args.Operation.Level,
04d4c92d0f28 Improved logging
cin
parents: 130
diff changeset
113 Message = entry.ToString(),
92
4c0e5ef99986 rewritten tracing
cin
parents: 85
diff changeset
114 Thread = args.ThreadId,
134
04d4c92d0f28 Improved logging
cin
parents: 130
diff changeset
115 Channel = args.ChannelName,
48
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
116 Timestamp = Environment.TickCount
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
117 };
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
118
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
119 Enqueue(item);
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
120 }
47
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
121 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
122 }