-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
287 lines (222 loc) · 8.63 KB
/
Form1.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using tp1;
using tpfinal;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Diagnostics;
namespace tpfinal
{
public partial class Form1 : Form
{
private int ocurrencias = 1;
private int speed = 1;
private int finalizados = 0;
private double tiempo_total = 0.0;
private string resultado = "";
private string planifSelected = "";
private Scheduling planificador = new Scheduling(new List<Proceso>());
private List<ListItem> lista = new List<ListItem>();
private List<System.ComponentModel.BackgroundWorker> backgroundWorkers = new List<BackgroundWorker>();
private List<Stopwatch> clocks = new List<Stopwatch>();
public Form1()
{
InitializeComponent();
}
private void btnclose_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button1_Click_1(object sender, EventArgs e)
{
List<Proceso> collected = new List<Proceso>();
finalizados = 0;
tiempo_total = 0.0;
resultado = "";
Backend.buscar(heapOp.Checked, collected);
if (collected.Count > 0)
{
planifSelected = heapOp.Checked ? heapOp.Text : otroOp.Text;
flowLayoutPanel1.Controls.Clear();
backgroundWorkers.Clear();
lista.Clear();
planificador = new Scheduling(collected);
for (int i = 1; i <= ocurrencias; i++)
{
ListItem item = new ListItem(i);
item.Width = flowLayoutPanel1.Width - 25;
lista.Add(item);
flowLayoutPanel1.Controls.Add(item);
addBackgroundWorker();
clocks.Add(new Stopwatch());
}
for (int i = 0; i < ocurrencias; i++)
{
backgroundWorkers[i].RunWorkerAsync(i);
}
inSimulation();
}
}
private void inSimulation()
{
cntrlBox.Enabled = false;
planGroupBox.Enabled = false;
button1.Enabled = false;
btnNo.Enabled = true;
}
private void addBackgroundWorker()
{
System.ComponentModel.BackgroundWorker backgroundWorker1=new System.ComponentModel.BackgroundWorker();
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
backgroundWorker1.WorkerReportsProgress=true;
backgroundWorker1.WorkerSupportsCancellation=true;
backgroundWorkers.Add(backgroundWorker1);
}
private void btnNo_Click(object sender, EventArgs e)
{
finalizados = 0;
for (int i = 0; i < ocurrencias; i++)
{
backgroundWorkers[i].CancelAsync();
}
}
[DllImport("user32.DLL", EntryPoint = "ReleaseCapture")]
private extern static void ReleaseCapture();
[DllImport("user32.DLL", EntryPoint = "SendMessage")]
private extern static void SendMessage(System.IntPtr hWnd, int wMsg, int wParam, int lParam);
private void barra_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, 0x112, 0xf012, 0);
}
private void btn_consulta1_Click(object sender, EventArgs e)
{
string resultado = Backend.todasLasPredicciones();
this.mostrarConsulta(resultado);
}
private void btn_consulta2_Click(object sender, EventArgs e)
{
string resultado = Backend.caminoAPrediccion();
this.mostrarConsulta(resultado);
}
private void btn_consulta3_Click(object sender, EventArgs e)
{
string resultado = Backend.aProfundidad();
this.mostrarConsulta(resultado);
}
private void mostrarConsulta(string resultado)
{
Consultas consulta = new Consultas(this, resultado);
consulta.Show();
this.Hide();
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
txtDist.Text = "CPU's: " + trackBar1.Value;
ocurrencias = trackBar1.Value;
}
private void backgroundWorker1_DoWork(object sender,
DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
long tiempo = ComputeCPU((int)e.Argument, worker, e);
Tuple<int, long> tupla = new Tuple<int, long>((int)e.Argument, tiempo);
e.Result = tupla;
}
private void backgroundWorker1_RunWorkerCompleted(
object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show(e.Error.Message);
initForSimulation();
}
else if (e.Cancelled)
{
finalizados++;
if (finalizados == ocurrencias)
{
resultado = "";
MessageBox.Show("Simulacion Reiniciada.", "Reinicio", MessageBoxButtons.OK, MessageBoxIcon.Warning);
initForSimulation();
}
}
else
{
Tuple<int, long> tupla = (Tuple<int, long>) e.Result;
resultado += "Tiempo de ejecucion total del CPU #" + (tupla.Item1 + 1) + ": " + (tupla.Item2 / 1000) + " segs" + '\n';
tiempo_total = tupla.Item2 / 1000;
finalizados++;
if (finalizados==ocurrencias)
{
resultado += "Total Nro de CPU's " +ocurrencias+ '\n';
resultado += "Tiempo total promedio " + (tiempo_total/ocurrencias) + " segs";
MessageBox.Show(resultado, "Resultados de la planificación "+ planifSelected, MessageBoxButtons.OK, MessageBoxIcon.Information);
initForSimulation();
}
}
}
private void initForSimulation()
{
cntrlBox.Enabled = true;
planGroupBox.Enabled = true;
button1.Enabled = true;
btnNo.Enabled = false;
flowLayoutPanel1.Controls.Clear();
backgroundWorkers.Clear();
lista.Clear();
}
private void backgroundWorker1_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
Tuple<int, Proceso> tupla = (Tuple<int, Proceso>)e.UserState;
int id = tupla.Item1;
Proceso process = tupla.Item2;
ListItem item = lista[id];
if (process!=null)
{
item.Dato = process;
item.StepValue(e.ProgressPercentage);
}
else
{
item.IdleState();
}
item.Update();
}
long ComputeCPU(int id, BackgroundWorker worker, DoWorkEventArgs e)
{
Proceso process = planificador.getProceso();
ListItem item = lista[id];
clocks[id].Start();
Tuple<int, Proceso> tupla = new Tuple<int, Proceso>(id, process);
int i = 0;
int speedFactor = 1;
if (speed > 1)
{
speedFactor = speed * 40;
}
while (process != null && !worker.CancellationPending)
{
tupla = new Tuple<int, Proceso>(id, process);
i = 0;
while (i < process.tiempo && !worker.CancellationPending) {
worker.ReportProgress(i, tupla);
System.Threading.Thread.Sleep(200/speedFactor);
i++;
}
process = planificador.getProceso();
}
tupla = new Tuple<int, Proceso>(id, null);
worker.ReportProgress(0, tupla);
e.Cancel = worker.CancellationPending;
clocks[id].Stop();
return clocks[id].ElapsedMilliseconds;
}
private void trackBar2_Scroll(object sender, EventArgs e)
{
txtSpeed.Text = "Speed: " + trackBar2.Value+"x";
speed = trackBar2.Value;
}
}
}