-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kerncentrale.cs
120 lines (110 loc) · 3.6 KB
/
Kerncentrale.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using Windows.System.Threading;
using ThreadPool = System.Threading.ThreadPool;
namespace Kerncentrale
{
class Kerncentrale
{
private List<Reactor> reactors;
private ThreadingType threadingType;
private ThreadPoolTimer timer;
public Kerncentrale(ThreadingType threadingType)
{
this.reactors = new List<Reactor>();
this.threadingType = threadingType;
this.InitializeTmpReactors();
this.InitialiseTimer();
}
public List<Reactor> GetReactors()
{
return this.reactors;
}
/// <summary>
/// timer that increases temp increase for every reactor/ fuelrod every 10 seconds
/// </summary>
private void InitialiseTimer()
{
timer = ThreadPoolTimer.CreatePeriodicTimer(TimerElapsedHandler, new TimeSpan(0, 0, 10));
}
private void TimerElapsedHandler(ThreadPoolTimer timer)
{
foreach (var reactor in reactors)
{
reactor.IncTempIncrease(10);
}
}
/// <summary>
/// Returns total produced energy of all the reactors.
/// </summary>
/// <returns></returns>
public double getTotalEnergy()
{
double t = 0;
foreach (var reactor in reactors)
{
t += reactor.GetTotalEnergy();
}
Debug.WriteLine(t);
return t;
}
/// <summary>
/// Set random fuelRods to the reactor
/// </summary>
public void InitializeTmpReactors()
{
Random rnd = new Random();
for (int i = 0; i < 5; i++)
{
Reactor reactor = new Reactor();
reactor.SetSelectedThreadingType(this.threadingType);
int rndNumber = rnd.Next(5, 10);
for (int j = 0; j < rndNumber; j++)
{
if (rndNumber % 2 == 1)
reactor.AddFuelRod(new FuelRod.Uranium());
else
reactor.AddFuelRod(new FuelRod.Plutonium());
}
reactors.Add(reactor);
}
}
/// <summary>
/// setup for threads
/// </summary>
public void GenerateThreads()
{
try
{
int i = 1;
foreach (Reactor reactor in this.reactors)
{
switch (reactor.selectedThreadingType)
{
case ThreadingType.SingleThreading:
reactor.ExecuteThread();
break;
case ThreadingType.MultiThreading:
Thread thread = new Thread(reactor.ExecuteThread)
{
Name = reactor.ToString() + i
};
thread.Start();
break;
case ThreadingType.ThreadPool:
ThreadPool.QueueUserWorkItem(reactor.ThreadProcess);
break;
}
Thread.Sleep(100);
i++;
}
}
catch (Exception e)
{
Debug.WriteLine("\n\n\n\nException\nmessage: {0}\nStacktrace: {1}", e.Message, e.StackTrace);
}
}
}
}