-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFormMain.cs
More file actions
126 lines (107 loc) · 4.34 KB
/
FormMain.cs
File metadata and controls
126 lines (107 loc) · 4.34 KB
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
using System;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TaskProgressExample
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
this.Text = Application.ProductName;
this.StartPosition = FormStartPosition.CenterScreen;
var tableLayout = new TableLayoutPanel
{
Dock = DockStyle.Fill,
RowCount = 4,
ColumnCount = 1,
Padding = new Padding(20),
};
Controls.Add(tableLayout);
tableLayout.RowStyles.Add(new RowStyle(SizeType.Absolute, 70));
var buttonExample1 = new Button
{
Dock = DockStyle.Fill,
Margin = new Padding(10),
Font = new Font("Arial", 10),
Text = "Example 1: no progress report, no cancel button",
};
buttonExample1.Click += ButtonExample1_Click;
tableLayout.Controls.Add(buttonExample1);
tableLayout.RowStyles.Add(new RowStyle(SizeType.Absolute, 70));
var buttonExample2 = new Button
{
Dock = DockStyle.Fill,
Margin = new Padding(10),
Font = new Font("Arial", 10),
Text = "Example 2: has progress report, no cancel button",
};
buttonExample2.Click += ButtonExample2_Click;
tableLayout.Controls.Add(buttonExample2);
tableLayout.RowStyles.Add(new RowStyle(SizeType.Absolute, 70));
var buttonExample3 = new Button
{
Dock = DockStyle.Fill,
Margin = new Padding(10),
Font = new Font("Arial", 10),
Text = "Example 3: has progress report, has cancel button",
};
buttonExample3.Click += ButtonExample3_Click;
tableLayout.Controls.Add(buttonExample3);
tableLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 100));
}
private async void ButtonExample1_Click(object sender, EventArgs e)
{
int n = 50;
var result = await RunTask("Example 1", "No progress report, please wait for 5 seconds.", n, 0, false);
MessageBox.Show("The " + n + "th Fibonacci number is " + result, "Finished!");
}
private async void ButtonExample2_Click(object sender, EventArgs e)
{
int n = 50;
var result = await RunTask("Example 2", "", n, n, false);
MessageBox.Show("The " + n + "th Fibonacci number is " + result, "Finished!");
}
private async void ButtonExample3_Click(object sender, EventArgs e)
{
int n = 50;
var result = await RunTask("Example 3", "", n, n, true);
if(result > 0)
MessageBox.Show("The " + n + "th Fibonacci number is " + result, "Finished!");
else
MessageBox.Show("The calculation has been cancelled.", "Cancelled!");
}
public async Task<long> RunTask(string info1, string info2, int n, int maxProgress, bool isCancellable)
{
long result;
using (var formProgress = new FormProgress(info1, info2, maxProgress, isCancellable))
{
formProgress.Show();
result = await Task.Run(() => Fibonacci(n,
maxProgress > 0 ? formProgress.Progress : null,
isCancellable ? formProgress.CancellationTokenSource : null));
formProgress.Close();
}
return result;
}
public static long Fibonacci(int len, IProgress<string> progress, CancellationTokenSource cancellationTokenSource)
{
long a = 0, b = 1, c = 0;
for (int i = 2; i < len; i++)
{
if (cancellationTokenSource != null && cancellationTokenSource.Token.IsCancellationRequested)
return 0;
// For demonstration only
Thread.Sleep(100);
c = a + b;
a = b;
b = c;
if (progress != null)
progress.Report(i.ToString() + "|" + c.ToString());
}
return c;
}
}
}