annotate Implab.Diagnostics.Interactive/InteractiveListener.cs @ 47:b181f7bcb259 interactive logger

refactoring, interactive tarce log almost complete
author cin
date Thu, 17 Apr 2014 18:49:36 +0400
parents
children d9d794b61bb9
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 {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
12 public class InteractiveListener: Disposable
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;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
17
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
18 readonly IPromiseBase m_guiFinished;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
19 readonly Promise<object> m_guiStarted = new Promise<object>();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
20
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
21 readonly IPromiseBase m_workerFinished = new Promise<object>();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
22
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
23 readonly MTQueue<TraceViewItem> m_queue = new MTQueue<TraceViewItem>();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
24 readonly AutoResetEvent m_queueEvent = new AutoResetEvent(false);
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
25
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
26 int m_queueLength;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
27 bool m_exitPending;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
28
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
29 readonly object m_pauseLock = new object();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
30 bool m_paused;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
31 readonly ManualResetEvent m_pauseEvent = new ManualResetEvent(true);
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
32
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
33 public InteractiveListener() {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
34 m_guiFinished = AsyncPool.InvokeNewThread(() => {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
35 GuiThread();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
36 return 0;
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 m_guiStarted.Join();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
40 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
41
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
42 void GuiThread() {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
43 m_form = new TraceForm(); // will create SynchronizationContext
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
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
58 m_syncGuiThread.Send(x => m_form.AddTraceEvent(item),null);
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
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
65 public void Pause() {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
66 // for consistency we need to set this properties atomically
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
67 lock (m_pauseLock) {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
68 m_pauseEvent.Reset();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
69 m_paused = true;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
70 }
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 public void Resume() {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
74 // for consistency we need to set this properties atomically
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
75 lock (m_pauseLock) {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
76 m_paused = false;
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
77 m_pauseEvent.Set();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
78 }
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 void Enqueue(TraceViewItem item) {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
82 m_queue.Enqueue(item);
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
83 if (Interlocked.Increment(ref m_queueLength) == 1)
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
84 m_queueEvent.Set();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
85 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
86
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
87 public void ShowForm() {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
88 m_syncGuiThread.Post(x => m_form.Show(), null);
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
89 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
90
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
91 public void HideForm() {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
92 m_syncGuiThread.Post(x => m_form.Hide(), null);
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
93 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
94
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
95 void Terminate() {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
96 m_syncGuiThread.Post(x => Application.ExitThread(), null);
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
97 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
98
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
99 protected override void Dispose(bool disposing) {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
100 if (disposing) {
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
101 Terminate();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
102 m_guiFinished.Join();
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
103 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
104 base.Dispose(disposing);
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
105 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
106 }
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents:
diff changeset
107 }