annotate Implab.Diagnostics.Interactive/InteractiveListener.cs @ 112:38d6a4db35d7 v2

fixed Promise.Error handler
author cin
date Wed, 19 Nov 2014 13:34:09 +0300
parents 4c0e5ef99986
children 671f60cd0250
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 {
48
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
12 public class InteractiveListener: TextListenerBase
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;
48
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
17 readonly Promise<object> m_guiStarted = new Promise<object>();
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
48
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
32 public InteractiveListener(bool global) : base(global) {
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
33 m_guiFinished = AsyncPool.InvokeNewThread(GuiThread);
85
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 66
diff changeset
34 /*m_workerFinished = */AsyncPool.InvokeNewThread(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
92
4c0e5ef99986 rewritten tracing
cin
parents: 85
diff changeset
110 protected override void WriteEntry(LogEventArgs args, EventText text, string channel) {
48
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
111 var item = new TraceViewItem {
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
112 Indent = text.indent,
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
113 Message = text.content,
92
4c0e5ef99986 rewritten tracing
cin
parents: 85
diff changeset
114 Thread = args.ThreadId,
48
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
115 Channel = channel,
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 }