changeset 215:fe5101083150 v2

Fixed InteractiveListener to support OLE and clipboard. Safe.Dispose methods made more convinent
author cin
date Fri, 21 Apr 2017 14:27:17 +0300
parents 9c32ef39b851
children 1e082fb67a46
files Implab.Diagnostics.Interactive/InteractiveListener.cs Implab/Safe.cs
diffstat 2 files changed, 41 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/Implab.Diagnostics.Interactive/InteractiveListener.cs	Fri Apr 14 15:57:23 2017 +0300
+++ b/Implab.Diagnostics.Interactive/InteractiveListener.cs	Fri Apr 21 14:27:17 2017 +0300
@@ -17,7 +17,6 @@
         readonly Promise m_guiStarted = new Promise();
         
         readonly IPromise m_guiFinished;
-        //        readonly IPromise m_workerFinished = new Promise<object>();
         
         readonly MTQueue<TraceViewItem> m_queue = new MTQueue<TraceViewItem>();
         readonly AutoResetEvent m_queueEvent = new AutoResetEvent(false);
@@ -30,8 +29,8 @@
         readonly ManualResetEvent m_pauseEvent = new ManualResetEvent(true);
 
         public InteractiveListener() {
-            m_guiFinished = AsyncPool.RunThread(GuiThread);
-            /*m_workerFinished = */AsyncPool.RunThread(QueueThread);
+            m_guiFinished = RunGuiThread();
+            AsyncPool.RunThread(QueueThread);
 
             m_guiStarted.Join();
         }
@@ -63,6 +62,31 @@
             }
         }
 
+        public IPromise RunGuiThread() {
+            var p = new Promise();
+
+            var caller = TraceContext.Instance.CurrentOperation;
+
+            var worker = new Thread(() => {
+                TraceContext.Instance.EnterLogicalOperation(caller, false);
+                try {
+                    Application.OleRequired();
+                    GuiThread();
+                    p.Resolve();
+                } catch (Exception e) {
+                    p.Reject(e);
+                } finally {
+                    TraceContext.Instance.Leave();
+                }
+            });
+            worker.SetApartmentState(ApartmentState.STA);
+            worker.IsBackground = true;
+            worker.Name = string.Format("{0} GUI Thread", nameof(InteractiveListener));
+            worker.Start();
+
+            return p;
+        }
+
         public void Pause() {
             // for consistency we need to set this properties atomically
             lock (m_pauseLock) {
--- a/Implab/Safe.cs	Fri Apr 14 15:57:23 2017 +0300
+++ b/Implab/Safe.cs	Fri Apr 21 14:27:17 2017 +0300
@@ -65,23 +65,31 @@
             }
         }
 
-        public static void Dispose(IEnumerable<IDisposable> objects) {
+        public static void DisposeCollection(IEnumerable<IDisposable> objects) {
             foreach (var d in objects)
-                if (d != null)
-                    d.Dispose();
+                Dispose(d);
+        }
+
+        public static void DisposeCollection(IEnumerable objects) {
+            foreach (var d in objects)
+                Dispose(d);
         }
 
         public static void Dispose(object obj) {
-            var d = obj as IDisposable;
-            if (d != null)
-                d.Dispose();
+            if (obj is IDisposable) {
+                Dispose((IDisposable)obj);
+            } else if (obj is IEnumerable) {
+                Dispose((IEnumerable)obj);
+            }
         }
 
+        [DebuggerStepThrough]
         public static void DispatchEvent<T>(this EventHandler<T> handler, object sender, T args) {
             if (handler != null)
                 handler(sender, args);
         }
 
+        [DebuggerStepThrough]
         public static void DispatchEvent(this EventHandler handler, object sender, EventArgs args) {
             if (handler != null)
                 handler(sender, args);