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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
| class LabelInfo
{
public string Text { get; }
public Color Color { get; }
public Point Position { get; }
public LabelInfo(string text, Color color, Point position)
{
if (text == null)
{
throw new ArgumentNullException(nameof(text));
}
Text = text;
Color = color;
Position = position;
}
}
class LabelAddedEventArgs : EventArgs
{
public string Text { get; }
public Color Color { get; }
public LabelAddedEventArgs(string text, Color color)
{
Text = text;
Color = color;
}
}
interface ILabelManager
{
Point CurrentPosition { set; }
void AddLabel(string text, Color color);
event EventHandler ContentAdded;
IEnumerable<LabelInfo> Labels { get; }
}
class LabelManager : ILabelManager
{
private List<LabelInfo> _labels = new List<LabelInfo>();
public Point CurrentPosition { private get; set; }
public void AddLabel(string text, Color color)
{
if (text == null)
{
throw new ArgumentNullException(nameof(text));
}
_labels.Add(new LabelInfo(text, color, CurrentPosition));
ContentAdded?.Invoke(this, EventArgs.Empty);
}
public IEnumerable<LabelInfo> Labels => _labels.ToList();
public event EventHandler ContentAdded;
}
interface IThreadManager
{
int MaxThreads { set; }
int MaxCounterValue { set; }
bool TryAddThread();
event EventHandler<LabelAddedEventArgs> LabelAdded;
}
class ThreadManager : IThreadManager
{
private TimeSpan _delay;
public ThreadManager(TimeSpan delay)
{
_delay = delay;
}
private int _maxThreads;
public int MaxThreads
{
private get { return _maxThreads; }
set
{
if (value < 0)
{
throw new ArgumentOutOfRangeException(nameof(value));
}
_maxThreads = value;
}
}
private int _maxCounterValue;
public int MaxCounterValue
{
private get { return _maxCounterValue; }
set
{
if (value < 1)
{
throw new ArgumentOutOfRangeException(nameof(value));
}
_maxCounterValue = value;
}
}
private void GenerateLabels(Color color)
{
int counter = 1;
while (counter <= MaxCounterValue)
{
Thread.Sleep(_delay);
if (counter <= MaxCounterValue)
{
LabelAdded?.Invoke(this, new LabelAddedEventArgs(counter.ToString(), color));
counter += 1;
}
}
FinishGeneration();
}
private void FinishGeneration()
{
lock (_counterLocker)
{
_currentThreadsCounter -= 1;
if (_currentThreadsCounter == 0)
{
Monitor.PulseAll(_counterLocker);
}
while (_currentThreadsCounter > 0)
{
Monitor.Wait(_counterLocker);
}
}
}
public event EventHandler<LabelAddedEventArgs> LabelAdded;
private int _currentThreadsCounter;
private object _counterLocker = new object();
public bool TryAddThread()
{
Color[] colors = { Color.Red, Color.Green, Color.Blue, Color.Black, Color.Beige };
Color color;
lock (_counterLocker)
{
if (_currentThreadsCounter >= MaxThreads)
{
return false;
}
color = colors[_currentThreadsCounter % colors.Length];
_currentThreadsCounter += 1;
};
new Thread(_ => GenerateLabels(color)) { IsBackground = true }.Start();
return true;
}
}
interface IModel
{
bool TryAddThread();
Point CursorPosition { set; }
int MaxThreads { set; }
int MaxCounterValue { set; }
IEnumerable<LabelInfo> Labels { get; }
event EventHandler ContentChanged;
}
class Model : IModel
{
private ILabelManager _labelManager;
private IThreadManager _threadManager;
private object _locker = new object();
public Model(ILabelManager labelManager, IThreadManager threadManager)
{
if (labelManager == null)
{
throw new ArgumentNullException(nameof(labelManager));
}
if (threadManager == null)
{
throw new ArgumentNullException(nameof(threadManager));
}
_labelManager = labelManager;
_threadManager = threadManager;
_labelManager.ContentAdded += delegate { ContentChanged?.Invoke(this, EventArgs.Empty); };
_threadManager.LabelAdded += (_, e) =>
{
lock (_locker)
{
_labelManager.AddLabel(e.Text, e.Color);
}
};
}
public bool TryAddThread()
{
return _threadManager.TryAddThread();
}
public Point CursorPosition
{
set { _labelManager.CurrentPosition = value; }
}
public int MaxThreads
{
set { _threadManager.MaxThreads = value; }
}
public int MaxCounterValue
{
set { _threadManager.MaxCounterValue = value; }
}
public IEnumerable<LabelInfo> Labels
{
get
{
lock (_locker)
{
return _labelManager.Labels;
}
}
}
public event EventHandler ContentChanged;
}
class MainForm : Form
{
private IModel _model;
public MainForm()
{
MouseClick += HandleMouseClick;
MouseMove += HandleMouseMove;
base.DoubleBuffered = true;
base.Width = 400;
base.MinimumSize = new Size(400, 200);
}
private void BuildControls()
{
foreach (var control in Controls.Cast<Control>())
{
control.Dispose();
}
var lblMaxThreads = new Label { Text = "Максимум потоков:", Width = 200, Parent = this };
var nudMaxThreads = new NumericUpDown { Minimum = 0, Increment = 1, Left = lblMaxThreads.Right + Padding.All, Parent = this };
nudMaxThreads.ValueChanged += (sender, _) =>
{
int value = (int)((NumericUpDown)sender).Value;
_model.MaxThreads = value;
};
nudMaxThreads.Value = 1;
var lblMaxCounterValue = new Label { Text = "Максимальное значение счётчика:", Width = 200, Top = lblMaxThreads.Bottom + Padding.All, Parent = this };
var nudMaxCounterValue = new NumericUpDown
{
Minimum = 1,
Increment = 1,
Left = lblMaxCounterValue.Right + Padding.All,
Top = lblMaxCounterValue.Top,
Parent = this
};
nudMaxCounterValue.ValueChanged += (sender, _) =>
{
int value = (int)((NumericUpDown)sender).Value;
_model.MaxCounterValue = value;
};
nudMaxCounterValue.Value = 5;
}
private void HandleMouseMove(object sender, EventArgs e)
{
if (_model == null)
{
return;
}
var mouse = (MouseEventArgs)e;
_model.CursorPosition = mouse.Location;
}
private void HandleMouseClick(object sender, EventArgs e)
{
if (_model == null)
{
return;
}
var mouse = (MouseEventArgs)e;
if (mouse.Button != MouseButtons.Left)
{
return;
}
_model.TryAddThread();
}
public void Build(IModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_model = model;
_model.ContentChanged += delegate { BeginInvoke(new Action(() => Invalidate())); };
BuildControls();
}
protected override void OnPaint(PaintEventArgs e)
{
using (var font = new Font(FontFamily.GenericSansSerif, 30))
{
foreach (var labelInfo in _model.Labels)
{
using (var brush = new SolidBrush(labelInfo.Color))
{
e.Graphics.DrawString(labelInfo.Text, font, brush, new PointF(labelInfo.Position.X, labelInfo.Position.Y));
}
}
}
}
}
void Main()
{
ILabelManager labelManager = new LabelManager();
IThreadManager threadManager = new ThreadManager(TimeSpan.FromMilliseconds(500));
IModel model = new Model(labelManager, threadManager);
var form = new MainForm();
form.Build(model);
Application.Run(form);
} |