-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.cs
61 lines (55 loc) · 1.87 KB
/
tasks.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
namespace CS_ExamenStuff
{
public partial class sTTForm : Form
{
IStringStuff stuff = new StringStuff();
private void decodeButton_Click(object sender, EventArgs e)
{
stuff.PctChangedEvent += updateProgress;
stuff.doStuff("This is an example with parameters");
}
private void updateProgress(decimal x)
{
progressBar1.Value = (int)(Math.Ceiling(x));
}
private void cancelButton_Click(object sender, EventArgs e)
{
if (stuff.cancelSource != null)
{
stuff.cancelSource.Cancel();
}
}
}
}
namespace LogicImplementation
{
public class StringStuff: IStringStuff
{
private CancellationToken cancelToken;
public CancellationTokenSource cancelSource { get; set; }
public event Action<decimal> PctChangedEvent;
public void doStuff(string exampleString)
{
cancelSource = new CancellationTokenSource();
cancelToken = cancelSource.Token;
var decRep = new Progress<decimal>((decCount) =>
{
if (PctChangedEvent != null) PctChangedEvent(decCount);
});
Task.Run(() => { doStuffTask(exampleString, decRep, cancelToken); });
}
public void doStuffTask(string exampleString, IProgress<decimal> decRep, CancellationToken token)
{
int twenty = 20;
string whatever = "";
for (int i = 0; i < twenty; i++)
{
if (!token.IsCancellationRequested)
{
whatever += exampleString;
decRep.Report((Math.Ceiling((decimal)(i + 1) * 100 / twenty)));
}
}
} // public void doStuffTask
} // public class StringStuff
} // namespace LogicImplementation