private void btnRead_PN_Click(object sender, EventArgs e) { try { this.txtPN.Text = dut.ReadPn(); } catch (Exception ex) { MessageBox.Show(ex.Message, "No link.", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void toolStripBtnRun_Click(object sender, EventArgs e) { try { if (this.radioButtonPowerCyclesTest.Checked) { return; } this.Icon = Properties.Resources.OnLineBusy; this.toolStripBtnRun.Enabled = false; decimal cycles = this.numericUpDownCycles.Value; status = 0; string sn = dut.ReadPn(); int count = 0; int delay = (int)this.numericUpDownDelay.Value; Dictionary <string, int> dic = new Dictionary <string, int>(); dic.Add(sn, count); Task task = Task.Factory.StartNew(() => { for (int i = 1; i < cycles; i++) { Thread.Sleep(delay); if (status == -1) { return; } sn = dut.ReadPn(); if (!dic.Keys.Contains(sn)) { dic.Add(sn, ++count); } string label = count + " / " + (i + 1); if (this.InvokeRequired) { this.BeginInvoke(new UpdateControl(delegate { this.labelResult.Text = label; }), label); } else { this.labelResult.Text = label; } } }); Task cwt = task.ContinueWith(t => { if (this.InvokeRequired) { this.BeginInvoke(new MethodInvoker(delegate { string message = "Serial number-->"; foreach (string key in dic.Keys) { message += key + " "; } this.Icon = Properties.Resources.Online; this.toolStripBtnRun.Enabled = true; MessageBox.Show(message, "how many serial number", MessageBoxButtons.OK, MessageBoxIcon.Information); })); } else { string message = "Serial number-->"; foreach (string key in dic.Keys) { message += key + " "; } this.Icon = Properties.Resources.Online; this.toolStripBtnRun.Enabled = true; MessageBox.Show(message, "how many serial number", MessageBoxButtons.OK, MessageBoxIcon.Information); } }); } catch (Exception ex) { this.Icon = Properties.Resources.Online; this.toolStripBtnRun.Enabled = true; MessageBox.Show("No link. please click read buton fistly on tha main GUI", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }