public IProgressVisualizer CreateVisualizer(ProgressWorkerParams workerParams) { _params = workerParams; btnCancel.Enabled = workerParams.AllowCancel; _asyncOp.Post( () => { Location = GetLocation(); if (!Visible) { if (!Modal) { Show(); } else { ShowDialog(); } } }); return(this); }
void Run() { if (_running) { MessageBox.Show( "The neural network is training. Wait until done.", "Already running", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } ProgressWorkerParams workerParams = new ProgressWorkerParams { Alignment = System.Drawing.ContentAlignment.BottomLeft, AllowCancel = true, ControlAnchor = frmMain.GInstance.MActiveChart, Modal = true, }; ProgressWorker.Run( workerParams, visualizer => { try { visualizer.SetProgressTitle("Neural Network"); visualizer.SetProgressAction("Initializing..."); AsyncNeuralNetwork worker = new AsyncNeuralNetwork(); bool first = true; worker.ProgressCallback += (object o, int records, int record, ref bool stop) => { if (first) { first = false; visualizer.InitProgress(0, records); } visualizer.ReportProgress(record); stop = visualizer.CancelReqested; }; visualizer.SetProgressAction("Training Neural Network..."); List <string> items = new List <string>(); foreach (NListBoxItem item in lstSelected.Items) { items.Add(item.Text); } // Do the work worker.DoWork(items); // Exit if no report if (worker.ResultCount < 1 || visualizer.CancelReqested) { return; // No work to report } visualizer.SetProgressAction("Completing..."); visualizer.InitProgress(0, worker.ResultCount); for (int n = 0; n < worker.ResultCount; ++n) { visualizer.ReportProgress(n); if (visualizer.CancelReqested) { break; } } } catch (Exception ex) { _asyncOp.Post( () => MessageBox.Show( "An error occured. Error :" + Environment.NewLine + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)); } finally { _running = false; } }, () => { }); }
void UseThisFunctionToDoTheAsyncWork() { if (_running) { MessageBox.Show( "The work is in progress. Wait until done.", "Already running", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } ProgressWorkerParams workerParams = new ProgressWorkerParams { Alignment = System.Drawing.ContentAlignment.BottomLeft, AllowCancel = true, ControlAnchor = this, Modal = true, }; ProgressWorker.Run( workerParams, visualizer => { try { visualizer.SetProgressTitle("Feature name goes here"); visualizer.SetProgressAction("Working..."); YourWorkerClass worker = new YourWorkerClass(); bool first = true; worker.ProgressCallback += (object o, int records, int record, ref bool stop) => { if (first) { first = false; visualizer.InitProgress(0, records); } visualizer.ReportProgress(record); stop = visualizer.CancelReqested; }; visualizer.SetProgressAction("Initializing..."); // Do the work worker.DoWork(); // Exit if no report if (worker.ResultCount < 1 || visualizer.CancelReqested) return; // No work to report visualizer.SetProgressAction("Completing..."); visualizer.InitProgress(0, worker.ResultCount); for(int n = 0; n < worker.ResultCount; ++n) { visualizer.ReportProgress(n); if (visualizer.CancelReqested) break; } } catch (Exception ex) { _asyncOp.Post( () => MessageBox.Show( "An error occured. Error :" + Environment.NewLine + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)); } finally { _running = false; } }, () => { }); }
public IProgressVisualizer CreateVisualizer(ProgressWorkerParams workerParams) { return(_progressForm.CreateVisualizer(workerParams)); }