private void btnRun_Click(object sender, EventArgs e) { if (toolToExecute == null) { return; } toolToExecute.Initialize(); if (!toolToExecute.Initialized) { return; } DotSpatial.Modeling.Forms.ToolProgress progForm = new DotSpatial.Modeling.Forms.ToolProgress(1); if (toolToExecute.MultiThreadRequired) { //We create a background worker thread to execute the tool BackgroundWorker bw = new BackgroundWorker(); bw.DoWork += BwDoWork; bw.RunWorkerCompleted += ExecutionComplete; object[] threadParameter = new object[2]; threadParameter[0] = toolToExecute; threadParameter[1] = progForm; // Show the progress dialog and kick off the Async thread progForm.Show(this); bw.RunWorkerAsync(threadParameter); } else { try { System.Windows.Forms.Cursor.Current = Cursors.WaitCursor; toolToExecute.Execute(progForm); toolToExecute.AfterExecution(null); System.Windows.Forms.Cursor.Current = Cursors.Default; } catch (Exception ex) { _MessageService.ShowError(null, ex.Message); } } }
private static void BwDoWork(object sender, DoWorkEventArgs e) { object[] threadParameter = e.Argument as object[]; if (threadParameter == null) { return; } IModelTool toolToExecute = threadParameter[0] as IModelTool; ToolProgress progForm = threadParameter[1] as ToolProgress; if (progForm == null) { return; } if (toolToExecute == null) { return; } progForm.Progress(String.Empty, 0, "=================="); progForm.Progress(String.Empty, 0, String.Format("Executing Tool: {0}", toolToExecute.Name)); progForm.Progress(String.Empty, 0, "=================="); try { toolToExecute.Execute(progForm); } catch (Exception ex) { progForm.Progress(String.Empty, 100, "Failed to run. Errors:" + ex.Message); } finally { progForm.ExecutionComplete(); progForm.Progress(String.Empty, 100, "=================="); progForm.Progress(String.Empty, 100, String.Format("Done Executing Tool: {0}", toolToExecute.Name)); progForm.Progress(String.Empty, 100, "=================="); } }