-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTestForm.cs
141 lines (122 loc) · 4.12 KB
/
TestForm.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
#region using
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using ThreadGun;
#endregion
namespace TestThreadingMethod
{
public partial class TestForm : Form
{
private const int NumCount = 20000;
private ThreadGun<int> _tg;
public TestForm()
{
InitializeComponent();
}
private void btnNormal_Click(object sender, EventArgs e)
{
lstThread.Items.Clear();
foreach (var i in Enumerable.Range(1, NumCount)) ActionThread(i);
}
private void btnThread_Click(object sender, EventArgs e)
{
lstThread.Items.Clear();
new Thread(() =>
{
foreach (var i in Enumerable.Range(1, NumCount)) ActionThread(i);
}).Start();
}
private void btnThreadGun_Click(object sender, EventArgs e)
{
lstThreadGunResult.Items.Clear();
_tg = new ThreadGun<int>((Action<int>) ActionThreadGun, Enumerable.Range(1, NumCount), 20,
tg_ExceptionOccurred);
_tg.Completed += tg_Completed;
_tg.FillingMagazine();
_tg.Start();
}
private void tg_ExceptionOccurred(ThreadGun<int> gun, IEnumerable<int> inputs, object input,
Exception exception)
{
MessageBox.Show($@"Exception Occurred!!!
Message :
{exception.Message}
Input :
{(int) input}");
}
private void tg_Completed(object inputs)
{
MessageBox.Show(@"ThreadGun Process Completed!");
}
private void btnThreadPool_Click(object sender, EventArgs e)
{
lstThreadPoolResult.Items.Clear();
ThreadPool.SetMaxThreads(20, 20);
ThreadPool.SetMinThreads(20, 20);
foreach (var i in Enumerable.Range(1, NumCount))
ThreadPool.QueueUserWorkItem(ActionThreadPool, i);
}
public void ActionThread(int i)
{
Invoke(new MethodInvoker(delegate
{
lstThreadGunResult.Items.Add($@"> {i} <");
lblInfoThread.Text = $@"Item Count : {lstThread.Items.Count}";
}));
Application.DoEvents();
}
public void ActionThreadGun(int i)
{
Invoke(new MethodInvoker(delegate
{
lstThreadGunResult.Items.Add($@"> {i} <");
lblInfoThreadGun.Text = $@"Item Count : {lstThreadGunResult.Items.Count}";
}));
Application.DoEvents();
if (i == 250)
throw new Exception("ExceptionOccurred Test!");
//if (i == 500)
//{
// MessageBox.Show(@"I'll wait ten minutes");
// _tg.Wait(10 * 60 * 1000, () => MessageBox.Show(@"I continue process"));
//}
}
public void ActionThreadPool(object i)
{
try
{
if ((int) i == NumCount)
MessageBox.Show(@"ThreadPool Process Completed!");
Invoke(new MethodInvoker(delegate
{
lstThreadPoolResult.Items.Add($@"> {i} <");
lblInfoThreadPool.Text = $@"Item Count : {lstThreadPoolResult.Items.Count}";
}));
Application.DoEvents();
if ((int) i == 250)
throw new Exception("ExceptionOccurred Test!");
}
catch (Exception exception)
{
MessageBox.Show($@"Exception Occurred!!!
Message :
{exception.Message}
Input :
{i}");
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Environment.Exit(0);
}
private void btnRefresh_Click(object sender, EventArgs e)
{
lblInfoThreadPool.Text = $@"Item Count : {lstThreadPoolResult.Items.Count}";
lblInfoThreadGun.Text = $@"Item Count : {lstThreadGunResult.Items.Count}";
lblInfoThread.Text = $@"Item Count : {lstThread.Items.Count}";
}
}
}