ui/wpf/UIcore/Application.cs

changeset 135
b9dc9cdfa23a
parent 89
9a7e4a335b2b
--- a/ui/wpf/UIcore/Application.cs	Sun Jan 22 11:48:50 2017 +0100
+++ b/ui/wpf/UIcore/Application.cs	Sun Jan 22 17:23:20 2017 +0100
@@ -15,21 +15,19 @@
         private System.Windows.Application application;
 
         private Thread thread;
-        private Queue<Action> queue = new Queue<Action>();
-        private object sync = new object();
-        private object result = new object();
-        private Boolean main = false;
 
         public String Name;
+
+        public IApplicationCallbacks callbacks;
+
         public List<Window> Windows = new List<Window>();
         public ApplicationMenu Menu = new ApplicationMenu();
         public MainToolBar ToolBar = new MainToolBar();
         
         private Application() : base()
         {
-            thread = new Thread(() => Run());
+            thread = new Thread(() => RunApplication());
             thread.SetApartmentState(ApartmentState.STA);
-            thread.Start();
         }
 
         public static Application GetInstance()
@@ -44,86 +42,22 @@
 
         public Thread Start()
         {
-            lock(sync)
-            {
-                queue.Enqueue(() => RunApplication());
-                Monitor.Pulse(sync);
-            }
+            thread.Start();
             return thread;
         }
 
         private void RunApplication()
         {
             application = new System.Windows.Application();
-            main = true;
-            application.Run();
-        }
 
-        public void Run()
-        {
-            lock(sync)
-            {
-                for (;;)
-                {
-                    Monitor.Wait(sync);
-                    Action action = queue.Dequeue();
-                    action.Invoke();
-                    lock (result)
-                    {
-                        Monitor.Pulse(result);
-                    }
-                    if (main)
-                    {
-                        // end loop after shutdown
-                        break;
-                    }
-                }
-            }
-        }
-
-        public T Exec<T>(Func<T> func)
-        {
-            ResultExec<T> e = new ResultExec<T>();
-            e.Func = func;
-
-            if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
+            if(callbacks != null)
             {
-                e.Exec();
-                return e.Result;
-            }
-            else
-            {
-                lock (sync)
-                {
-                    queue.Enqueue(() => e.Exec());
-                    Monitor.Pulse(sync);
-                }
-                lock (result)
-                {
-                    Monitor.Wait(result);
-                }
-
-                return e.Result;
+                callbacks.OnStartup();
             }
-        }
-
-        public void Exec(Action action)
-        {
-            if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
-            {
-                action.Invoke();
-            }
-            else
+            application.Run();
+            if(callbacks != null)
             {
-                lock (sync)
-                {
-                    queue.Enqueue(action);
-                    Monitor.Pulse(sync);
-                }
-                lock (result)
-                {
-                    Monitor.Wait(result);
-                }
+                callbacks.OnExit();
             }
         }
 
@@ -162,4 +96,11 @@
             Action.Invoke();
         }
     }
+
+    public interface IApplicationCallbacks
+    {
+        void OnStartup();
+        void OnOpen();
+        void OnExit();
+    }
 }

mercurial