-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrmPiDay.cs
133 lines (115 loc) · 3.3 KB
/
FrmPiDay.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
using Pfz.Math;
using PiDay.Utils.Calculator;
using System.Diagnostics;
namespace PiDay;
public partial class FrmPiDay : Form
{
private Thread thread;
private volatile bool evaluating = false;
private volatile bool interrupted = false;
public FrmPiDay()
{
InitializeComponent();
}
private void EvalPI(int method, int decimalDigits)
{
try
{
StepCalculator calculator = method switch
{
// Super Lento
0 => new VerySlowPICalculator(decimalDigits, 2),
// Rápido
1 => new MachinPICalculator(decimalDigits),
// Super Rápido
2 => new GaussLegendrePICalculator(decimalDigits),
_ => throw new Exception($"Método não reconhecido: {method}"),
};
calculator.OnProgress += EvalPIProgress;
calculator.OnComplete += EvalPIComplete;
while (!interrupted && !calculator.Step()) { }
}
catch (ThreadInterruptedException)
{
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
Debug.WriteLine(e.StackTrace);
}
finally
{
if (interrupted)
EvalPIInterrupted();
}
}
private void FrmPiDay_Load(object sender, EventArgs e)
{
cmbMethod.SelectedIndex = 2;
}
private void EvalPIProgress(float progress, BigDecimal currentEval, int computedDigits)
{
if (InvokeRequired)
{
BeginInvoke(EvalPIProgress, [progress, currentEval, computedDigits]);
return;
}
pbProgress.Value = (int) (progress * 100);
}
private void EvalPIComplete(BigDecimal pi, int decimalDigits)
{
if (InvokeRequired)
{
BeginInvoke(EvalPIComplete, [pi, decimalDigits]);
return;
}
string s = pi.ToString();
int p = s.IndexOf('.');
s = string.Concat(s.AsSpan(0, p), ".", s.AsSpan(p + 1, decimalDigits));
txtPI.Text = s;
evaluating = false;
interrupted = false;
pbProgress.Visible = false;
btnCalculatePI.Text = "Calcule π";
}
private void EvalPIInterrupted()
{
if (InvokeRequired)
{
BeginInvoke(EvalPIInterrupted);
return;
}
interrupted = false;
evaluating = false;
pbProgress.Visible = false;
btnCalculatePI.Text = "Calcule π";
}
private void BtnCalculatePI_Click(object sender, EventArgs e)
{
if (evaluating)
{
interrupted = true;
}
else
{
btnCalculatePI.Text = "Cancelar";
pbProgress.Visible = true;
pbProgress.Value = 0;
int decimalDigits = int.Parse(txtDecimalDigits.Text);
int method = cmbMethod.SelectedIndex;
evaluating = true;
interrupted = false;
thread = new Thread(() => EvalPI(method, decimalDigits));
thread.Start();
}
}
private void FrmPiDay_FormClosing(object sender, FormClosingEventArgs e)
{
interrupted = true;
if (thread != null)
{
thread.Interrupt();
thread = null;
}
}
}