##// END OF EJS Templates
Fixed broken Implab.Diagnostics.Interactive
cin -
r204:cbb0bd8fc0d1 v2
parent child
Show More
@@ -1,122 +1,122
1 1 using Implab.Parallels;
2 2 using System;
3 3 using System.Collections.Generic;
4 4 using System.Linq;
5 5 using System.Text;
6 6 using System.Threading;
7 7 using System.Threading.Tasks;
8 8 using System.Windows.Forms;
9 9
10 10 namespace Implab.Diagnostics.Interactive
11 11 {
12 12 public class InteractiveListener: ListenerBase
13 13 {
14 14 TraceForm m_form;
15 15
16 16 SynchronizationContext m_syncGuiThread;
17 17 readonly Promise m_guiStarted = new Promise();
18 18
19 19 readonly IPromise m_guiFinished;
20 20 // readonly IPromise m_workerFinished = new Promise<object>();
21 21
22 22 readonly MTQueue<TraceViewItem> m_queue = new MTQueue<TraceViewItem>();
23 23 readonly AutoResetEvent m_queueEvent = new AutoResetEvent(false);
24 24
25 25 int m_queueLength;
26 26 bool m_exitPending;
27 27
28 28 readonly object m_pauseLock = new object();
29 29 bool m_paused;
30 30 readonly ManualResetEvent m_pauseEvent = new ManualResetEvent(true);
31 31
32 32 public InteractiveListener() {
33 33 m_guiFinished = AsyncPool.RunThread(GuiThread);
34 34 /*m_workerFinished = */AsyncPool.RunThread(QueueThread);
35 35
36 36 m_guiStarted.Join();
37 37 }
38 38
39 39 void GuiThread() {
40 40 m_form = new TraceForm(); // will create SynchronizationContext
41 41
42 42 m_form.PauseEvents += (s,a) => Pause();
43 43 m_form.ResumeEvents += (s, a) => Resume();
44 44
45 45 m_syncGuiThread = SynchronizationContext.Current;
46 46 m_guiStarted.Resolve();
47 47 Application.Run();
48 48 }
49 49
50 50 void QueueThread() {
51 51 while (!m_exitPending) {
52 52 if (m_paused)
53 53 m_pauseEvent.WaitOne();
54 54
55 55 TraceViewItem item;
56 56 if (m_queue.TryDequeue(out item)) {
57 57 Interlocked.Decrement(ref m_queueLength);
58 58
59 59 m_syncGuiThread.Send(x => m_form.AddTraceEvent(item),null);
60 60 } else {
61 61 m_queueEvent.WaitOne();
62 62 }
63 63 }
64 64 }
65 65
66 66 public void Pause() {
67 67 // for consistency we need to set this properties atomically
68 68 lock (m_pauseLock) {
69 69 m_pauseEvent.Reset();
70 70 m_paused = true;
71 71 }
72 72 }
73 73
74 74 public void Resume() {
75 75 // for consistency we need to set this properties atomically
76 76 lock (m_pauseLock) {
77 77 m_paused = false;
78 78 m_pauseEvent.Set();
79 79 }
80 80 }
81 81
82 82 void Enqueue(TraceViewItem item) {
83 83 m_queue.Enqueue(item);
84 84 if (Interlocked.Increment(ref m_queueLength) == 1)
85 85 m_queueEvent.Set();
86 86 }
87 87
88 88 public void ShowForm() {
89 89 m_syncGuiThread.Post(x => m_form.Show(), null);
90 90 }
91 91
92 92 public void HideForm() {
93 93 m_syncGuiThread.Post(x => m_form.Hide(), null);
94 94 }
95 95
96 96 void Terminate() {
97 97 m_exitPending = true;
98 98 Resume();
99 99 m_syncGuiThread.Post(x => Application.ExitThread(), null);
100 100 }
101 101
102 102 protected override void Dispose(bool disposing) {
103 103 if (disposing) {
104 104 Terminate();
105 105 m_guiFinished.Join();
106 106 }
107 107 base.Dispose(disposing);
108 108 }
109 109
110 110 public override void Write(LogEventArgs args, object entry) {
111 111 var item = new TraceViewItem {
112 112 Indent = args.Operation.Level,
113 113 Message = entry.ToString(),
114 114 Thread = args.ThreadId,
115 Channel = args.ChannelName,
115 Channel = args.Channel.ToString(),
116 116 Timestamp = Environment.TickCount
117 117 };
118 118
119 119 Enqueue(item);
120 120 }
121 121 }
122 122 }
@@ -1,53 +1,54
1 1 using System;
2 2 using System.Collections.Generic;
3 3 using System.ComponentModel;
4 4 using System.Data;
5 5 using System.Drawing;
6 6 using System.Linq;
7 7 using System.Text;
8 8 using System.Threading.Tasks;
9 9 using System.Windows.Forms;
10 10
11 11 namespace Implab.Diagnostics.Interactive {
12 12 public partial class TraceForm : Form {
13 13 readonly Dictionary<int, Color> m_threadColors = new Dictionary<int,Color>();
14 14 readonly Random m_rand = new Random();
15 15
16 16 public event EventHandler PauseEvents;
17 17
18 18 public event EventHandler ResumeEvents;
19 19
20 20 public TraceForm() {
21 21 InitializeComponent();
22 22 }
23 23
24 24 protected override void OnFormClosing(FormClosingEventArgs e) {
25 25 base.OnFormClosing(e);
26 26 if (!e.Cancel && e.CloseReason == CloseReason.UserClosing) {
27 27 e.Cancel = true;
28 28 Hide();
29 29 }
30 30 }
31 31
32 32 public void AddTraceEvent(TraceViewItem item) {
33 33 traceViewItemBindingSource.Add(item);
34 if(eventsDataGrid.RowCount > 0)
34 35 eventsDataGrid.FirstDisplayedScrollingRowIndex = eventsDataGrid.RowCount - 1;
35 36 }
36 37
37 38 Color GetThreadColor(int thread) {
38 39 Color result;
39 40 if (!m_threadColors.TryGetValue(thread, out result)) {
40 41 result = Color.FromArgb(m_rand.Next(4)*64, m_rand.Next(4)*64, m_rand.Next(4)*64);
41 42 m_threadColors[thread] = result;
42 43 }
43 44 return result;
44 45 }
45 46
46 47 private void eventsDataGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
47 48 var data = (TraceViewItem)traceViewItemBindingSource[e.RowIndex];
48 49 if (e.ColumnIndex == messageDataGridViewTextBoxColumn.Index)
49 50 e.CellStyle.Padding = new Padding(data.Indent * 10,0,0,0);
50 51 e.CellStyle.ForeColor = GetThreadColor(data.Thread);
51 52 }
52 53 }
53 54 }
General Comments 3
Under Review
author

Auto status change to "Under Review"

Approved
author

ok, latest stable version should be in default

You need to be logged in to leave comments. Login now