-
Notifications
You must be signed in to change notification settings - Fork 5
/
App.xaml.cs
144 lines (125 loc) · 6.05 KB
/
App.xaml.cs
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
141
142
143
144
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Diagnostics;
namespace DiReCT
{
/// <summary>
/// The main entry point for the application
/// </summary>
public partial class DiReCTMainProgram: Application
{
private static Thread[] moduleThreadHandles = new Thread[(int)ThreadRequiredModule.NumberOfModules];
//private static Thread UIThreadHandle;
private static ModuleControlData[] modulesControlData = new ModuleControlData[(int)ThreadRequiredModule.NumberOfModules];
private static AutoResetEvent[] moduleReadyEvents = new AutoResetEvent[(int)ThreadRequiredModule.NumberOfModules];
private static bool notInitializationTimeout = true; // Whether initialization processes were completed in time
private void DiReCTStarup (object sender, StartupEventArgs startupEventArgs)
{
Debug.WriteLine("HI");
// Initialize thread objects of modules
moduleThreadHandles[(int)ThreadRequiredModule.AAA] = new Thread(AAAModule.AAAInit);
moduleThreadHandles[(int)ThreadRequiredModule.DM] = new Thread(DMModule.DMInit);
moduleThreadHandles[(int)ThreadRequiredModule.DS] = new Thread(DSModule.DSInit);
moduleThreadHandles[(int)ThreadRequiredModule.MAN] = new Thread(MANModule.MANInit);
moduleThreadHandles[(int)ThreadRequiredModule.RTQC] = new Thread(RTQCModule.RTQCInit);
// Initialize the thread object of UI thread
//UIThreadHandle = new Thread(UIMainFunction);
//UIThreadHandle.SetApartmentState(ApartmentState.STA);
// Initialize control data of modules
for (int i=0;i<(int)ThreadRequiredModule.NumberOfModules;i++)
{
modulesControlData[i] = new ModuleControlData();
}
// Start to execute modules
try
{
moduleThreadHandles[(int)ThreadRequiredModule.AAA].Start(modulesControlData[(int)ThreadRequiredModule.AAA].ThreadParameters);
moduleThreadHandles[(int)ThreadRequiredModule.DM].Start(modulesControlData[(int)ThreadRequiredModule.DM].ThreadParameters);
moduleThreadHandles[(int)ThreadRequiredModule.DS].Start(modulesControlData[(int)ThreadRequiredModule.DS].ThreadParameters);
moduleThreadHandles[(int)ThreadRequiredModule.MAN].Start(modulesControlData[(int)ThreadRequiredModule.MAN].ThreadParameters);
moduleThreadHandles[(int)ThreadRequiredModule.RTQC].Start(modulesControlData[(int)ThreadRequiredModule.RTQC].ThreadParameters);
}
catch (OutOfMemoryException e)
{
Debug.WriteLine(e.Message);
Debug.WriteLine("Some module thread cannot start.");
goto InitializationFail;
}
// Set up the array of Ready events
for (int i = 0; i < (int)ThreadRequiredModule.NumberOfModules; i++)
{
moduleReadyEvents[i] = modulesControlData[i].ThreadParameters.ReadyToWorkEvent;
}
System.Threading.Timer timerOfIntialization = new System.Threading.Timer(new TimerCallback(InitializationTimeOutEventHandler),
null,
(int)TimeInterval.LongTime,
Timeout.Infinite);
while (notInitializationTimeout)
{
if (WaitHandle.WaitAll(moduleReadyEvents, (int)TimeInterval.VeryShortTime, true))
{
Debug.WriteLine("Phase 1 initialization of all modules complete!");
break;
}
else
{
for (int i = 0; i < moduleThreadHandles.Length; i++)
{
Thread moduleThreadHandle = moduleThreadHandles[i];
if (!moduleThreadHandle.IsAlive)
{
Debug.WriteLine("Phase 1 initialization of " + i + "-th module fails!");
notInitializationTimeout = false;
goto InitializationFail;
}
}
}
}
timerOfIntialization.Dispose();
foreach (ModuleControlData moduleControlData in modulesControlData)
{
moduleControlData.ThreadParameters.StartWorkEvent.Set();
}
//try
//{
// UIThreadHandle.Start(coreControl);
//}
//catch (OutOfMemoryException e)
//{
// Debug.WriteLine(e.Message);
// Debug.WriteLine("UI thread can not start!");
// goto InitializationFail;
//}
//UIThreadHandle.Join();
Return:
// singal all created thread to prepare to terminate
foreach (ModuleControlData moduleControlData in modulesControlData)
{
moduleControlData.ThreadParameters.TerminateWorkEvent.Set();
}
// wait for all created threads to be terminated
foreach (Thread moduleThreadHandle in moduleThreadHandles)
{
if (moduleThreadHandle.ThreadState != System.Threading.ThreadState.Unstarted)
{
moduleThreadHandle.Join();
}
}
return;
InitializationFail:
goto Return;
// Turn into UI thread
MainWindow = new MainWindow();
MainWindow.Show();
}
private static void InitializationTimeOutEventHandler(object state)
{
notInitializationTimeout = false;
}
}
}