void OnOperationComplete(DeviceLoader sender, Operation operation, bool withSuccess) { if (InvokeRequired) { BeginInvoke(new Action(() => { OnOperationComplete(sender, operation, withSuccess); })); return; } // Safe to work with UI UpdateUI(String.Format("{0} complete.", operation)); // TODO: Handle the results of the operation if ((operation == Operation.Reading) && withSuccess) { SaveFileDialog dlg = new SaveFileDialog(); dlg.DefaultExt = "rom"; dlg.AddExtension = true; dlg.OverwritePrompt = true; dlg.Title = "Save ROM Image"; dlg.Filter = "ROM Image (*.rom)|*.rom"; DialogResult result = dlg.ShowDialog(); if (result == DialogResult.OK) { File.WriteAllBytes(dlg.FileName, m_loader.Data); } } // Clean up progress state m_ctlProgress.Value = m_ctlProgress.Minimum; }
void OnConnectionStateChanged(DeviceLoader sender, ConnectionState state) { if (InvokeRequired) { BeginInvoke(new Action(() => { OnConnectionStateChanged(sender, state); })); return; } // Safe to work with UI UpdateUI(String.Format("Device is {0}", state)); }
void OnProgress(DeviceLoader sender, ProgressState state, int position, int target, string message) { if (InvokeRequired) { BeginInvoke(new Action(() => { OnProgress(sender, state, position, target, message); })); return; } // Safe to work with UI m_ctlProgress.Maximum = target; m_ctlProgress.Value = position; UpdateUI(message); }
void OnError(DeviceLoader sender, string message, Exception ex) { if (InvokeRequired) { BeginInvoke(new Action(() => { OnError(sender, message, ex); })); return; } // Safe to work with UI if (ex != null) { message = message + "\n" + ex.ToString(); } MessageBox.Show(message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); }
public MainForm() { InitializeComponent(); // Set up the loader m_loader = new DeviceLoader(); m_loader.ConnectionStateChanged += OnConnectionStateChanged; m_loader.Progress += OnProgress; m_loader.Error += OnError; m_loader.OperationComplete += OnOperationComplete; UpdateUI(""); // Populate the list of available COM ports List <string> ports = new List <string>(SerialPort.GetPortNames()); ports.Sort(); m_ctlPorts.Items.AddRange(ports.ToArray()); if (m_ctlPorts.Items.Count > 0) { m_ctlPorts.SelectedIndex = 0; } }