/// <summary> /// The manager start function. /// </summary> public void startManager() { workers = new MTWorker[numWorkers]; // Creates the array to hold the workers. result = new ArrayList[numWorkers]; // Creates the array to hold the results. for (int i = 0; i < numWorkers; i++) { workers[i] = new MTWorker(i); // Creates a new worker with id=i. configureWorker(workers[i]); // Sets some properties to the worker. } AssignWorkers(); // Distribute work to each worker. }
/// <summary> /// Function to handle the end of work from a worker. /// </summary> /// <param name="sender">The worker who raised the event.</param> /// <param name="e">The argument of the event.</param> private void MTWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { MTWorker worker = sender as MTWorker; // Worker that raised the event. if (e.Error != null) // If there was an error during the parse. { if (!error) { error = true; foreach (MTWorker w in workers) // Cancel all the workers still running. { if (w.IsBusy) { w.WorkerSupportsCancellation = true; w.CancelAsync(); } } parentFormErrorCaller.DynamicInvoke(new Object[] { e.Error }); // Tell the parent Form there was an error. } } if (!error) { workersWorking = false; foreach (MTWorker w in workers) //Checks if there are still workers working. { if (w.IsBusy) { workersWorking = true; } } if (!workersWorking) // if not, call the function that will process the results, located in the caller form. { parentFormCaller.DynamicInvoke(new Object[] { xmlDoc }); } } }
/// <summary> /// Set some properties to the workers. /// </summary> /// <param name="MTW">The worker.</param> private void configureWorker(MTWorker MTW) { MTW.ProgressChanged += MTWorker_ProgressChanged; MTW.RunWorkerCompleted += MTWorker_RunWorkerCompleted; MTW.parentErrorCaller = parentFormErrorCaller; }