- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFormMain.cs
More file actions
Latest commit
126 lines (107 loc) · 4.34 KB
/
Copy pathFormMain.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
usingSystem;
usingSystem.Drawing;
usingSystem.Threading;
usingSystem.Threading.Tasks;
usingSystem.Windows.Forms;
namespaceTaskProgressExample
{
publicpartialclassFormMain:Form
{
publicFormMain()
{
InitializeComponent();
this.Text=Application.ProductName;
this.StartPosition=FormStartPosition.CenterScreen;
vartableLayout=newTableLayoutPanel
{
Dock=DockStyle.Fill,
RowCount=4,
ColumnCount=1,
Padding=newPadding(20),
};
Controls.Add(tableLayout);
tableLayout.RowStyles.Add(newRowStyle(SizeType.Absolute,70));
varbuttonExample1=newButton
{
Dock=DockStyle.Fill,
Margin=newPadding(10),
Font=newFont("Arial",10),
Text="Example 1: no progress report, no cancel button",
};
buttonExample1.Click+=ButtonExample1_Click;
tableLayout.Controls.Add(buttonExample1);
tableLayout.RowStyles.Add(newRowStyle(SizeType.Absolute,70));
varbuttonExample2=newButton
{
Dock=DockStyle.Fill,
Margin=newPadding(10),
Font=newFont("Arial",10),
Text="Example 2: has progress report, no cancel button",
};
buttonExample2.Click+=ButtonExample2_Click;
tableLayout.Controls.Add(buttonExample2);
tableLayout.RowStyles.Add(newRowStyle(SizeType.Absolute,70));
varbuttonExample3=newButton
{
Dock=DockStyle.Fill,
Margin=newPadding(10),
Font=newFont("Arial",10),
Text="Example 3: has progress report, has cancel button",
};
buttonExample3.Click+=ButtonExample3_Click;
tableLayout.Controls.Add(buttonExample3);
tableLayout.RowStyles.Add(newRowStyle(SizeType.Percent,100));
}
privateasyncvoidButtonExample1_Click(objectsender,EventArgse)
{
intn=50;
varresult=awaitRunTask("Example 1","No progress report, please wait for 5 seconds.",n,0,false);
MessageBox.Show("The "+n+"th Fibonacci number is "+result,"Finished!");
}
privateasyncvoidButtonExample2_Click(objectsender,EventArgse)
{
intn=50;
varresult=awaitRunTask("Example 2","",n,n,false);
MessageBox.Show("The "+n+"th Fibonacci number is "+result,"Finished!");
}
privateasyncvoidButtonExample3_Click(objectsender,EventArgse)
{
intn=50;
varresult=awaitRunTask("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!");
}
publicasyncTask<long>RunTask(stringinfo1,stringinfo2,intn,intmaxProgress,boolisCancellable)
{
longresult;
using(varformProgress=newFormProgress(info1,info2,maxProgress,isCancellable))
{
formProgress.Show();
result=awaitTask.Run(()=>Fibonacci(n,
maxProgress>0?formProgress.Progress:null,
isCancellable?formProgress.CancellationTokenSource:null));
formProgress.Close();
}
returnresult;
}
publicstaticlongFibonacci(intlen,IProgress<string>progress,CancellationTokenSourcecancellationTokenSource)
{
longa=0,b=1,c=0;
for(inti=2;i<len;i++)
{
if(cancellationTokenSource!=null&&cancellationTokenSource.Token.IsCancellationRequested)
return0;
// For demonstration only
Thread.Sleep(100);
c=a+b;
a=b;
b=c;
if(progress!=null)
progress.Report(i.ToString()+"|"+c.ToString());
}
returnc;
}
}
}