blob: fabfbaeae107ecb530de8e8b4995abad29d6dbae (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
#if NETFX_CORE
using System.Threading;
using System.Threading.Tasks;
using System.Reflection;
using System.IO;
using TP = System.Reflection.TypeInfo;
#else
using TP = System.Type;
#endif
namespace Pathfinding.WindowsStore {
public static class WindowsStoreCompatibility {
public static TP GetTypeInfo (System.Type type) {
#if NETFX_CORE
return type.GetTypeInfo();
#else
return type;
#endif
}
#if NETFX_CORE
public static void Close (this BinaryWriter stream) {
stream.Dispose();
}
public static void Close (this BinaryReader stream) {
stream.Dispose();
}
public static void Close (this StreamWriter stream) {
stream.Dispose();
}
#endif
}
#if NETFX_CORE
public delegate void ParameterizedThreadStart(System.Object ob);
public delegate void ThreadStart();
public class Thread {
//
// Fields
//
private Pathfinding.WindowsStore.ParameterizedThreadStart _paramThreadStart;
private CancellationTokenSource _taskCancellationTokenSource;
private Task _task = null;
private Pathfinding.WindowsStore.ThreadStart _threadStart;
private static ManualResetEvent SleepEvent = new ManualResetEvent(false);
//
// Properties
//
public bool IsAlive {
get {
return this._task != null && !this._task.IsCompleted;
}
set {
throw new System.NotImplementedException();
}
}
public bool IsBackground {
get {
return false;
}
set {
}
}
public string Name {
get;
set;
}
//
// Constructors
//
public Thread (Pathfinding.WindowsStore.ParameterizedThreadStart start) {
this._taskCancellationTokenSource = new CancellationTokenSource();
this._paramThreadStart = start;
}
public Thread (Pathfinding.WindowsStore.ThreadStart start) {
this._taskCancellationTokenSource = new CancellationTokenSource();
this._threadStart = start;
}
//
// Static Methods
//
public static void Sleep (int ms) {
SleepEvent.WaitOne(ms);
}
//
// Methods
//
public void Abort () {
if (this._taskCancellationTokenSource != null) {
this._taskCancellationTokenSource.Cancel();
}
}
private void EnsureTask (object paramThreadStartParam = null) {
if (this._task == null) {
if (this._paramThreadStart != null) {
this._task = new Task(delegate {
this._paramThreadStart(paramThreadStartParam);
}, this._taskCancellationTokenSource.Token);
} else {
if (this._threadStart != null) {
this._task = new Task(delegate {
this._threadStart();
}, this._taskCancellationTokenSource.Token);
}
}
}
}
public bool Join (int ms) {
this.EnsureTask();
return this._task.Wait(ms, this._taskCancellationTokenSource.Token);
}
public void Start () {
this.EnsureTask();
this._task.Start(TaskScheduler.Default);
}
public void Start (object param) {
this.EnsureTask(param);
this._task.Start(TaskScheduler.Default);
}
}
#endif
}
|