ui/wpf/UIcore/Application.cs

Sat, 24 Jan 2015 19:16:38 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 24 Jan 2015 19:16:38 +0100
changeset 79
052224670baf
parent 78
135920fe441b
child 81
5eb765a7a793
permissions
-rw-r--r--

configure script updated

78
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 using System;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 using System.Collections.Generic;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 using System.Linq;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 using System.Text;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 using System.Threading;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 using System.Threading.Tasks;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 namespace UI
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 public class Application
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 private static Application instance;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 private System.Windows.Application application;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 private Thread thread;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 private Queue<Action> queue = new Queue<Action>();
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 private object sync = new object();
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 private object result = new object();
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 private Boolean Running = false;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 public String Name;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 private Application() : base()
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 thread = new Thread(() => Run());
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 thread.SetApartmentState(ApartmentState.STA);
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28 thread.Start();
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 public static Application GetInstance()
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 if (instance == null)
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 instance = new Application();
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 GC.KeepAlive(instance);
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 return instance;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 public Thread Start()
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 lock(sync)
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 queue.Enqueue(() => RunApplication());
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 Monitor.Pulse(sync);
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 return thread;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 private void RunApplication()
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 application = new System.Windows.Application();
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 application.Run();
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 public void Run()
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 lock(sync)
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 for (;;)
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 Monitor.Wait(sync);
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 Action action = queue.Dequeue();
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 action.Invoke();
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 lock (result)
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 Monitor.Pulse(result);
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 public T Exec<T>(Func<T> func)
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 ResultExec<T> e = new ResultExec<T>();
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 e.Func = func;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 e.Exec();
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 return e.Result;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 else
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 lock (sync)
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 queue.Enqueue(() => e.Exec());
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 Monitor.Pulse(sync);
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 lock (result)
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 Monitor.Wait(result);
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 return e.Result;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 public void Exec(Action action)
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 action.Invoke();
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 else
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 lock (sync)
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 queue.Enqueue(action);
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 Monitor.Pulse(sync);
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 lock (result)
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 Monitor.Wait(result);
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 public class ResultExec<T>
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 public T Result;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 public Func<T> Func;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 public void Exec()
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 Result = Func.Invoke();
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 public class VoidExec
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 public Action Action;
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 public void Exec()
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 {
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 Action.Invoke();
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 }
135920fe441b added initial WPF code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 }

mercurial