// Group all the background methods in one place were it's easier to find #region BackGroundRegion public void bgw_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { // we cast the sender as being the thing that invokes this method as being a background worker BackgroundWorker bw = sender as BackgroundWorker; // used to extract the name of the sort engine name from the argument e.Argument string SortEngineName = (string)e.Argument; Type type = Type.GetType("SortingAlgorithmVisualizer." + SortEngineName); var constructor = type.GetConstructors(); try { ISortEngine se = (ISortEngine)constructor[0].Invoke(new object[] { unsortedArray, graphicsPanel, Graphics.Height }); // while the sorting process has not been finished or cancelled we will execute the NextStep method to keep going while (!se.isSorted() && (!executionThread.CancellationPending)) { se.NextStep(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public void bgw_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) //two arguments to event handler, what raised the event, and a block of arguments { BackgroundWorker bw = sender as BackgroundWorker; //explicitly identify sender as background worker string sortEngineName = (string)e.Argument; //now we know the name of the sort engine we would like to run Type type = Type.GetType("sortingAlgorithmVisualizer." + sortEngineName); //identifying of the class that we are going to create var ctors = type.GetConstructors(); //get the constructors of that type in our sorting engines //try-catch block in case something goes wrong try { ISortEngine se = (ISortEngine)ctors[0].Invoke(new object[] { theArray, g, panel1.Height }); //create a sort engine of the type identified and invoke its contructor /* We are creating a sort engine. We are invoking the first constructor in the list (there should only be one). * Pass to the constructor the list of the three parameters that we need. */ while (!se.isSorted() && (!bgw.CancellationPending)) { se.nextStep(); } } catch (Exception ex) { } }