HamBook – Blame information for rev 46

Subversion Repositories:
Rev:
Rev Author Line No. Line
15 office 1 using Configuration;
2 using HamBook.Properties;
3 using HamBook.Radios;
4 using HamBook.Radios.Generic;
41 office 5 using HamBook.Radios.Yaesu.FT_891;
16 office 6 using HamBook.Utilities.Serialization;
15 office 7 using Serilog;
8 using System;
9 using System.Collections;
10 using System.Collections.Concurrent;
11 using System.Collections.Generic;
12 using System.ComponentModel;
13 using System.Data;
14 using System.Diagnostics;
15 using System.Drawing;
16 using System.IO;
17 using System.Linq;
18 using System.Reflection;
19 using System.Text;
20 using System.Text.RegularExpressions;
21 using System.Threading;
22 using System.Threading.Tasks;
23 using System.Windows.Forms;
46 office 24 using MemoryChannel = HamBook.Radios.Generic.MemoryChannel;
15 office 25  
26 namespace HamBook
27 {
28 public partial class MemoryOrganizerForm : Form
29 {
30 private Configuration.Configuration Configuration { get; set; }
31  
32 private CatAssemblies _catAssemblies;
33 private CancellationToken _cancellationToken;
41 office 34 private Radios.Generic.MemoryBanks _memoryBanks;
22 office 35 private CancellationTokenSource _cancellationTokenSource;
36 private CancellationTokenSource _localCancellationTokenSource;
37 private CancellationToken _localCancellationToken;
15 office 38  
39 public MemoryOrganizerForm()
40 {
41 InitializeComponent();
22 office 42  
43 _localCancellationTokenSource = new CancellationTokenSource();
44 _localCancellationToken = _localCancellationTokenSource.Token;
15 office 45 }
46  
47 public MemoryOrganizerForm(Configuration.Configuration configuration, CatAssemblies catAssemblies, CancellationToken cancellationToken) : this()
48 {
49 Configuration = configuration;
50 _catAssemblies = catAssemblies;
22 office 51  
41 office 52 _cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_localCancellationToken, cancellationToken);
22 office 53 _cancellationToken = _cancellationTokenSource.Token;
41 office 54  
45 office 55 _memoryBanks = Radios.Generic.MemoryBanks.Create(Configuration.Radio);
15 office 56 }
57  
58 /// <summary>
59 /// Clean up any resources being used.
60 /// </summary>
61 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
62 protected override void Dispose(bool disposing)
63 {
64 if (disposing && (components != null))
65 {
22 office 66 if (_cancellationTokenSource != null)
67 {
68 _cancellationTokenSource.Cancel();
69 }
70  
15 office 71 components.Dispose();
72 }
73 base.Dispose(disposing);
74 }
75  
76 private async void button1_Click(object sender, EventArgs e)
77 {
41 office 78 var rows = dataGridView1.Rows.OfType<DataGridViewRow>().OrderBy(row => row.Index).ToList();
22 office 79 var count = rows.Count;
80  
81 toolStripProgressBar1.Minimum = 0;
82 toolStripProgressBar1.Maximum = count;
83  
84 var progress = new Progress<DataGridViewRowProgress>(rowProgress =>
85 {
86 try
87 {
88 switch (rowProgress)
89 {
90 case DataGridViewRowProgressSuccess<MemoryChannel> rowProgressSuccess:
46 office 91 var result = (Radios.Yaesu.FT_891.MemoryChannel)rowProgressSuccess.Data;
22 office 92  
93 rowProgress.Row.Cells["FrequencyColumn"].Value = result.Frequency;
94 rowProgress.Row.Cells["ClarifierDirectionColumn"].Value = (char)result.ClarifierDirection;
95 rowProgress.Row.Cells["ClarifierOffsetColumn"].Value = result.ClarifierOffset;
96 rowProgress.Row.Cells["ClarColumn"].Value = result.Clar;
44 office 97 rowProgress.Row.Cells["ModeColumn"].Value = result.MemoryRadioMode.Name;
46 office 98 rowProgress.Row.Cells["CtcssColumn"].Value = (string)result.Ctcss;
22 office 99 rowProgress.Row.Cells["PhaseColumn"].Value = (string)result.Phase;
100 rowProgress.Row.Cells["TagColumn"].Value = result.Tag;
26 office 101 rowProgress.Row.Cells["TextColumn"].Value = result.Text;
22 office 102 rowProgress.Row.Tag = rowProgressSuccess.Data;
103  
41 office 104  
105 toolStripStatusLabel1.Text = $"{Resources.Read_memory_bank} {rowProgress.Index + 1}";
22 office 106 break;
107 case DataGridViewRowProgressFailure rowProgressFailure:
108 Log.Error(rowProgressFailure.Exception, $"{Resources.Could_not_read_memory_bank}");
109  
41 office 110 toolStripStatusLabel1.Text = $"{Resources.Could_not_read_memory_bank} {rowProgress.Index + 1}";
22 office 111 break;
112 }
113  
114 toolStripProgressBar1.Increment(1);
115 statusStrip1.Update();
116 }
41 office 117 catch (Exception exception)
22 office 118 {
41 office 119 Log.Error(exception, Resources.Unexpected_error_while_reading_memory_bank);
22 office 120 }
121 });
122  
123 await Task.Run(() => ReadMemoryBanks(rows, progress, _cancellationToken), _cancellationToken);
124  
125 if (!_cancellationToken.IsCancellationRequested)
126 {
127 toolStripProgressBar1.Value = toolStripProgressBar1.Maximum;
128 toolStripStatusLabel1.Text = "Done.";
129 }
15 office 130 }
131  
132 private async void button2_Click(object sender, EventArgs e)
133 {
41 office 134 var rows = dataGridView1.Rows.OfType<DataGridViewRow>().OrderBy(row => row.Index).ToList();
22 office 135 var count = rows.Count;
136  
137 toolStripProgressBar1.Minimum = 0;
138 toolStripProgressBar1.Maximum = count;
139  
140 var progress = new Progress<DataGridViewRowProgress>(rowProgress =>
141 {
142 try
143 {
144 switch (rowProgress)
145 {
146 case DataGridViewRowProgressSuccess<bool> rowProgressSuccess:
147 var success = rowProgressSuccess.Data;
148  
41 office 149 if (success)
22 office 150 {
151  
152 toolStripStatusLabel1.Text =
41 office 153 $"{Resources.Wrote_memory_bank} {rowProgress.Index + 1}";
22 office 154 toolStripProgressBar1.Increment(1);
155 statusStrip1.Update();
156 return;
157 }
158  
41 office 159 Log.Error($"{Resources.Could_not_write_memory_bank}");
22 office 160 break;
161 case DataGridViewRowProgressFailure rowProgressFailure:
162 Log.Error(rowProgressFailure.Exception, $"{Resources.Could_not_write_memory_bank}");
163 break;
164 }
165  
41 office 166 toolStripStatusLabel1.Text =
167 $"{Resources.Could_not_write_memory_bank} {rowProgress.Index + 1}";
168 toolStripProgressBar1.Increment(1);
169 statusStrip1.Update();
22 office 170 }
41 office 171 catch (Exception exception)
172 {
173 Log.Error(exception, Resources.Unexpected_error_while_writing_memory_bank);
174 }
22 office 175  
176 });
177  
178 await Task.Run(() => WriteMemoryBanks(rows, progress, _cancellationToken), _cancellationToken);
179  
180 if (!_cancellationToken.IsCancellationRequested)
181 {
182 toolStripProgressBar1.Value = toolStripProgressBar1.Maximum;
183 toolStripStatusLabel1.Text = "Done.";
184 }
15 office 185 }
186  
187 private void MemoryOrganizerForm_Load(object sender, EventArgs e)
188 {
189 toolStripProgressBar1.Minimum = 0;
190 toolStripProgressBar1.Maximum = 98;
191  
41 office 192 var memoryBankQueue = new ConcurrentQueue<string>();
193 var memoryBankAddRowsTaskCompletionSource = new TaskCompletionSource<object>();
15 office 194  
195 async void IdleHandler(object idleHandlerSender, EventArgs idleHandlerArgs)
196 {
41 office 197 await memoryBankAddRowsTaskCompletionSource.Task;
15 office 198  
199 try
200 {
201 if (!memoryBankQueue.TryDequeue(out var memoryBank))
202 {
203 Application.Idle -= IdleHandler;
204  
205 dataGridView1.Sort(dataGridView1.Columns["LocationColumn"], ListSortDirection.Ascending);
206 toolStripStatusLabel1.Text = "Done.";
207  
208 return;
209 }
210  
211 var index = dataGridView1.Rows.Add();
212  
41 office 213 dataGridView1.Rows[index].Cells["LocationColumn"].Value = memoryBank;
15 office 214 dataGridView1.Rows[index].Cells["FrequencyColumn"].Value = default;
215 dataGridView1.Rows[index].Cells["ClarifierDirectionColumn"].Value = default;
216 dataGridView1.Rows[index].Cells["ClarifierOffsetColumn"].Value = default;
217 dataGridView1.Rows[index].Cells["ClarColumn"].Value = default;
218 dataGridView1.Rows[index].Cells["ModeColumn"].Value = default;
219 dataGridView1.Rows[index].Cells["CtcssColumn"].Value = default;
220 dataGridView1.Rows[index].Cells["PhaseColumn"].Value = default;
221 dataGridView1.Rows[index].Cells["TagColumn"].Value = default;
222 dataGridView1.Rows[index].Cells["TextColumn"].Value = default;
223  
224 toolStripStatusLabel1.Text = $"{Resources.Read_memory_bank} {memoryBank}";
225  
226 toolStripProgressBar1.Increment(1);
227  
228 statusStrip1.Update();
229 }
230 catch (Exception exception)
231 {
232 Log.Error(exception, Resources.Could_not_update_data_grid_view);
233 }
234 }
235  
236 Application.Idle += IdleHandler;
237 try
238 {
41 office 239 foreach (var memoryBank in _memoryBanks.GetMemoryBanks())
15 office 240 {
241 memoryBankQueue.Enqueue(memoryBank);
242 }
243  
41 office 244 memoryBankAddRowsTaskCompletionSource.TrySetResult(new { });
15 office 245 }
246 catch (Exception exception)
247 {
248 Application.Idle -= IdleHandler;
249  
250 Log.Error(exception, Resources.Unable_to_create_memory_banks);
251 }
252 }
253  
254 private void DataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
255 {
256 var dataGridView = (DataGridView)sender;
257  
258 if (dataGridView.CurrentCell is DataGridViewCheckBoxCell)
259 {
260 dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
261 }
262 }
263  
264 private void DataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
265 {
266 var dataGridView = (DataGridView)sender;
267  
268 dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
269 }
270  
271 private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
272 {
273 var dataGridView = (DataGridView)sender;
274  
275 if (dataGridView.CurrentCell is DataGridViewCheckBoxCell)
276 {
277 if (dataGridView.CurrentCell.IsInEditMode)
278 {
279 if (dataGridView.IsCurrentCellDirty)
280 {
281 dataGridView.EndEdit();
282 }
283 }
284 }
285 }
286  
287 private void DataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
288 {
289 var dataGridView = (DataGridView)sender;
290  
291 if (e.RowIndex == -1 || e.ColumnIndex == -1)
292 {
293 return;
294 }
295  
296 switch (dataGridView.Columns[e.ColumnIndex].Name)
297 {
298 case "EnableColumn":
299 //ProcessEnable(dataGridView.Rows[e.RowIndex]);
300 break;
301 }
302 }
303  
304 private void DataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
305 {
306 var dataGridView = (DataGridView)sender;
307  
308 if (e.RowIndex == -1 || e.ColumnIndex == -1 ||
309 !(dataGridView.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn))
310 {
311 return;
312 }
313  
314 dataGridView.EndEdit();
315 }
316  
317 private void DataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
318 {
319 var dataGridView = (DataGridView)sender;
320  
321 if (e.RowIndex == -1 || e.ColumnIndex == -1 ||
322 !(dataGridView.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn))
323 {
324 return;
325 }
326  
327 dataGridView.EndEdit();
328 }
329  
330 private void DataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
331 {
332 // @(-.-)@ -(o.o)- @(o_o)@
333 }
334  
335 private static List<DataGridViewRow> GetSelectedDataGridViewRows(DataGridView dataGridView)
336 {
337 return dataGridView.SelectedRows.OfType<DataGridViewRow>().OrderBy(row => row.Index).ToList();
338 }
339  
340 private async Task ReadMemoryBanks(IReadOnlyList<DataGridViewRow> rows, IProgress<DataGridViewRowProgress> progress,
341 CancellationToken cancellationToken)
342 {
343 var count = rows.Count;
344  
345 for (var i = 0; i < count && !cancellationToken.IsCancellationRequested; ++i)
346 {
347 try
348 {
41 office 349 var location = $"{rows[i].Cells["LocationColumn"].Value}";
15 office 350  
41 office 351 var result = await _catAssemblies.CatReadAsync<MemoryChannel>("MT", new object[] { location }, _cancellationToken);
15 office 352  
353 progress.Report(new DataGridViewRowProgressSuccess<MemoryChannel>(rows[i], i, result));
354 }
355 catch (UnexpectedRadioResponseException exception)
356 {
357 progress.Report(new DataGridViewRowProgressFailure(rows[i], i, exception));
358 }
359 catch (Exception exception)
360 {
361 progress.Report(new DataGridViewRowProgressFailure(rows[i], i, exception));
362 }
363 }
364 }
365  
366 private async Task WriteMemoryBanks(IReadOnlyList<DataGridViewRow> rows, IProgress<DataGridViewRowProgress> progress,
367 CancellationToken cancellationToken)
368 {
369 var count = rows.Count;
370  
371 for (var i = 0; i < count && !cancellationToken.IsCancellationRequested; ++i)
372 {
373 try
374 {
46 office 375 var memoryChannel = (Radios.Yaesu.FT_891.MemoryChannel)MemoryChannel.Create(Configuration.Radio);
15 office 376  
46 office 377 memoryChannel.CurrentLocation = $"{rows[i].Cells["LocationColumn"].Value}";
378 memoryChannel.Frequency = int.Parse($"{rows[i].Cells["FrequencyColumn"].Value}");
379 memoryChannel.ClarifierDirection = char.Parse($"{rows[i].Cells["ClarifierDirectionColumn"].Value}");
380 memoryChannel.ClarifierOffset = int.Parse($"{rows[i].Cells["ClarifierOffsetColumn"].Value}");
381 memoryChannel.Clar = Convert.ToBoolean(rows[i].Cells["ClarColumn"].Value);
382 memoryChannel.MemoryRadioMode = Radios.Generic.MemoryRadioMode.Create(Configuration.Radio, $"{rows[i].Cells["ModeColumn"].Value}");
383 memoryChannel.Ctcss = new Ctcss($"{rows[i].Cells["CtcssColumn"].Value}");
384 memoryChannel.Phase = new Phase($"{rows[i].Cells["PhaseColumn"].Value}");
385 memoryChannel.Tag = Convert.ToBoolean(rows[i].Cells["TagColumn"].Value);
386 memoryChannel.Text = $"{rows[i].Cells["TextColumn"].Value, -12}";
15 office 387  
388 var result = await _catAssemblies.CatSetAsync<MemoryChannel, bool>("MT", new object[] { memoryChannel }, _cancellationToken);
389  
390 progress.Report(new DataGridViewRowProgressSuccess<bool>(rows[i], i, result));
391 }
392 catch (UnexpectedRadioResponseException exception)
393 {
394 progress.Report(new DataGridViewRowProgressFailure(rows[i], i, exception));
395 }
396 catch (Exception exception)
397 {
398 progress.Report(new DataGridViewRowProgressFailure(rows[i], i, exception));
399 }
400 }
401 }
16 office 402  
403 private void importToolStripMenuItem_Click(object sender, EventArgs e)
404 {
405 openFileDialog1.ShowDialog();
406 }
407  
408 private void exportToolStripMenuItem_Click(object sender, EventArgs e)
409 {
410 saveFileDialog1.ShowDialog();
411 }
412  
413 private async void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
414 {
415 if (e.Cancel)
416 {
417 return;
418 }
419  
420 var fileName = saveFileDialog1.FileName;
26 office 421 var list = new List<MemoryChannelIndexed>();
16 office 422 foreach(var row in dataGridView1.Rows.OfType<DataGridViewRow>().OrderBy(row => row.Index))
423 {
46 office 424 if (row.Tag is Radios.Yaesu.FT_891.MemoryChannel memoryChannel)
16 office 425 {
26 office 426 var memoryChannelOrganizerBanks = new MemoryChannelIndexed(row.Index, memoryChannel);
16 office 427 list.Add(memoryChannelOrganizerBanks);
428 }
429 }
430  
431 var memoryBanks = list.ToArray();
432  
433 switch (await Serialization.Serialize(memoryBanks, fileName, "MemoryChannelOrganizerBank",
434 "<!ATTLIST MemoryChannelOrganizerBank xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>",
435 CancellationToken.None))
436 {
26 office 437 case SerializationSuccess<MemoryChannelIndexed[]> configuration:
16 office 438 Log.Information(Resources.Serialized_memory_banks);
439 break;
440 case SerializationFailure serializationFailure:
441 Log.Warning(serializationFailure.Exception.Message, Resources.Failed_to_serialize_memory_banks);
442 break;
443 }
444 }
445  
446 private async void openFileDialog1_FileOk(object sender, CancelEventArgs e)
447 {
448 if(e.Cancel)
449 {
450 return;
451 }
452  
453 var fileName = openFileDialog1.FileName;
26 office 454 MemoryChannelIndexed[] memoryBanks = null;
16 office 455  
456 var deserializationResult =
26 office 457 await Serialization.Deserialize<MemoryChannelIndexed[]>(fileName,
45 office 458 "urn:hambook-memorychannelorganizerbank-schema", "MemoryChannelIndexed.xsd", CancellationToken.None);
16 office 459  
460 switch (deserializationResult)
461 {
26 office 462 case SerializationSuccess<MemoryChannelIndexed[]> serializationSuccess:
16 office 463 Log.Information(Resources.Deserialized_memory_banks);
464 memoryBanks = serializationSuccess.Result;
465 break;
466 case SerializationFailure serializationFailure:
467 Log.Warning(serializationFailure.Exception, Resources.Failed_to_deserialize_memory_banks);
468 return;
469 }
470  
471 toolStripProgressBar1.Minimum = 0;
472 toolStripProgressBar1.Maximum = 98;
473  
26 office 474 var memoryBankQueue = new ConcurrentQueue<MemoryChannelIndexed>();
16 office 475 var snapshotsQueuedTaskCompletionSource = new TaskCompletionSource<object>();
476  
477 async void IdleHandler(object idleHandlerSender, EventArgs idleHandlerArgs)
478 {
479 await snapshotsQueuedTaskCompletionSource.Task;
480  
481 try
482 {
483 if (!memoryBankQueue.TryDequeue(out var memoryBank))
484 {
485 Application.Idle -= IdleHandler;
486  
487 dataGridView1.Sort(dataGridView1.Columns["LocationColumn"], ListSortDirection.Ascending);
488 toolStripStatusLabel1.Text = "Done.";
489  
490 return;
491 }
492  
493 dataGridView1.Rows[memoryBank.Index].Cells["FrequencyColumn"].Value = memoryBank.MemoryChannel.Frequency;
494 dataGridView1.Rows[memoryBank.Index].Cells["ClarifierDirectionColumn"].Value = (char)memoryBank.MemoryChannel.ClarifierDirection;
495 dataGridView1.Rows[memoryBank.Index].Cells["ClarifierOffsetColumn"].Value = memoryBank.MemoryChannel.ClarifierOffset;
496 dataGridView1.Rows[memoryBank.Index].Cells["ClarColumn"].Value = memoryBank.MemoryChannel.Clar;
44 office 497 dataGridView1.Rows[memoryBank.Index].Cells["ModeColumn"].Value = memoryBank.MemoryChannel.MemoryRadioMode.Name;
46 office 498 dataGridView1.Rows[memoryBank.Index].Cells["CtcssColumn"].Value = (string)memoryBank.MemoryChannel.Ctcss;
16 office 499 dataGridView1.Rows[memoryBank.Index].Cells["PhaseColumn"].Value = (string)memoryBank.MemoryChannel.Phase;
500 dataGridView1.Rows[memoryBank.Index].Cells["TagColumn"].Value = memoryBank.MemoryChannel.Tag;
501 dataGridView1.Rows[memoryBank.Index].Cells["TextColumn"].Value = memoryBank.MemoryChannel.Text;
46 office 502 dataGridView1.Rows[memoryBank.Index].Tag = memoryBank.MemoryChannel;
16 office 503  
504 toolStripProgressBar1.Increment(1);
505  
506 statusStrip1.Update();
507 }
508 catch (Exception exception)
509 {
510 Log.Error(exception, Resources.Could_not_update_data_grid_view);
511 }
512 }
513  
514 Application.Idle += IdleHandler;
515 try
516 {
517 foreach (var memoryChannel in memoryBanks)
518 {
519 memoryBankQueue.Enqueue(memoryChannel);
520 }
521  
522 snapshotsQueuedTaskCompletionSource.TrySetResult(new { });
523 }
524 catch (Exception exception)
525 {
526 Application.Idle -= IdleHandler;
527  
528 Log.Error(exception, Resources.Unable_to_create_memory_banks);
529 }
530 }
22 office 531  
532 private void MemoryOrganizerForm_FormClosing(object sender, FormClosingEventArgs e)
533 {
534 if(_cancellationTokenSource != null)
535 {
536 _cancellationTokenSource.Cancel();
537 }
538 }
40 office 539  
41 office 540 private async void readToolStripMenuItem_Click(object sender, EventArgs e)
40 office 541 {
41 office 542 var rows = GetSelectedDataGridViewRows(dataGridView1);
543  
544 var count = rows.Count;
545  
546 toolStripProgressBar1.Minimum = 0;
547 toolStripProgressBar1.Maximum = count;
548  
549 var progress = new Progress<DataGridViewRowProgress>(rowProgress =>
550 {
551 try
552 {
553 switch (rowProgress)
554 {
46 office 555 case DataGridViewRowProgressSuccess<Radios.Yaesu.FT_891.MemoryChannel> rowProgressSuccess:
556 var result = (Radios.Yaesu.FT_891.MemoryChannel)rowProgressSuccess.Data;
41 office 557  
558 rowProgress.Row.Cells["FrequencyColumn"].Value = result.Frequency;
559 rowProgress.Row.Cells["ClarifierDirectionColumn"].Value = (char)result.ClarifierDirection;
560 rowProgress.Row.Cells["ClarifierOffsetColumn"].Value = result.ClarifierOffset;
561 rowProgress.Row.Cells["ClarColumn"].Value = result.Clar;
44 office 562 rowProgress.Row.Cells["ModeColumn"].Value = result.MemoryRadioMode.Name;
46 office 563 rowProgress.Row.Cells["CtcssColumn"].Value = (string)result.Ctcss;
41 office 564 rowProgress.Row.Cells["PhaseColumn"].Value = (string)result.Phase;
565 rowProgress.Row.Cells["TagColumn"].Value = result.Tag;
566 rowProgress.Row.Cells["TextColumn"].Value = result.Text;
567 rowProgress.Row.Tag = rowProgressSuccess.Data;
568  
569  
570 toolStripStatusLabel1.Text = $"{Resources.Read_memory_bank} {rowProgress.Index + 1}";
571 break;
572 case DataGridViewRowProgressFailure rowProgressFailure:
573 Log.Error(rowProgressFailure.Exception, $"{Resources.Could_not_read_memory_bank}");
574  
575 toolStripStatusLabel1.Text = $"{Resources.Could_not_read_memory_bank} {rowProgress.Index + 1}";
576 break;
577 }
578  
579 toolStripProgressBar1.Increment(1);
580 statusStrip1.Update();
581 }
582 catch (Exception exception)
583 {
584 Log.Error(exception, Resources.Unexpected_error_while_reading_memory_bank);
585 }
586 });
587  
588 await Task.Run(() => ReadMemoryBanks(rows, progress, _cancellationToken), _cancellationToken);
589  
590 if (!_cancellationToken.IsCancellationRequested)
591 {
592 toolStripProgressBar1.Value = toolStripProgressBar1.Maximum;
593 toolStripStatusLabel1.Text = "Done.";
594 }
40 office 595 }
41 office 596  
597 private async void writeToolStripMenuItem_Click(object sender, EventArgs e)
598 {
599 var rows = GetSelectedDataGridViewRows(dataGridView1);
600 var count = rows.Count;
601  
602 toolStripProgressBar1.Minimum = 0;
603 toolStripProgressBar1.Maximum = count;
604  
605 var progress = new Progress<DataGridViewRowProgress>(rowProgress =>
606 {
607 try
608 {
609 switch (rowProgress)
610 {
611 case DataGridViewRowProgressSuccess<bool> rowProgressSuccess:
612 var success = rowProgressSuccess.Data;
613  
614 if (success)
615 {
616  
617 toolStripStatusLabel1.Text =
618 $"{Resources.Wrote_memory_bank} {rowProgress.Index + 1}";
619 toolStripProgressBar1.Increment(1);
620 statusStrip1.Update();
621 return;
622 }
623  
624 Log.Error($"{Resources.Could_not_write_memory_bank}");
625 break;
626 case DataGridViewRowProgressFailure rowProgressFailure:
627 Log.Error(rowProgressFailure.Exception, $"{Resources.Could_not_write_memory_bank}");
628 break;
629 }
630  
631 toolStripStatusLabel1.Text =
632 $"{Resources.Could_not_write_memory_bank} {rowProgress.Index + 1}";
633 toolStripProgressBar1.Increment(1);
634 statusStrip1.Update();
635 }
636 catch (Exception exception)
637 {
638 Log.Error(exception, Resources.Unexpected_error_while_writing_memory_bank);
639 }
640  
641 });
642  
643 await Task.Run(() => WriteMemoryBanks(rows, progress, _cancellationToken), _cancellationToken);
644  
645 if (!_cancellationToken.IsCancellationRequested)
646 {
647 toolStripProgressBar1.Value = toolStripProgressBar1.Maximum;
648 toolStripStatusLabel1.Text = "Done.";
649 }
650 }
15 office 651 }
652 }