private void OnWorkerProgressCompleted(object sender, RunWorkerCompletedEventArgs args) { LiveWireLateralWorker worker = sender as LiveWireLateralWorker; if (worker == null) { return; // TODO: Handle Error } if (args.Error != null) { throw new LiveWireWorkNotCompletedException("An error occurred in a LiveWire Worker", args.Error); } this.currentWorkersCompleted[currentWorkers.IndexOf(worker)] = true; if (!this.currentWorkersCompleted.Contains(false)) { this.Paths = new Dictionary <int, List <Point> >(); this.ControlPointIndices = new Dictionary <int, List <int> >(); this.TargetPathIndices = new Dictionary <int, TargetPathPoint>(); foreach (LiveWireLateralWorker w in this.currentWorkers) { foreach (var kvp in w.Paths) { this.Paths.Add(kvp.Key, kvp.Value); } foreach (var kvp in w.ControlPointIndices) { this.ControlPointIndices.Add(kvp.Key, kvp.Value); } foreach (var item in w.TargetPathIndices) { this.TargetPathIndices.Add(item.Key, item.Value); } } this.workCompleted = true; // Clear memory this.currentWorkers = null; GC.Collect(); GC.WaitForPendingFinalizers(); base.OnProgressCompleted(this, new RunWorkerCompletedEventArgs(null, null, false)); } }
public void ReRun() { // Assign width and height and other properties this.Width = Graph.Width; this.Height = Graph.Height; // Create thread workers currentWorkers = new List <LiveWireWorker>(); currentWorkersCompleted = new List <bool>(); currentWorkersProgress = new List <int>(); currentProgress = 0; for (int worker = 0; worker < ThreadCount; worker++) { LiveWireLateralWorker thread = new LiveWireLateralWorker(); thread.RunWorkerCompleted += new RunWorkerCompletedEventHandler(OnWorkerProgressCompleted); thread.ProgressChanged += new ProgressChangedEventHandler(OnWorkerProgressChanged); currentWorkers.Add(thread); currentWorkersCompleted.Add(false); currentWorkersProgress.Add(0); } // Create data objects for workers List <List <LiveWireLateralPath> > workerLateralPaths = new List <List <LiveWireLateralPath> >(); for (int t = 0; t < ThreadCount; t++) { workerLateralPaths.Add(new List <LiveWireLateralPath>()); } int currentThreadIndex = 0; foreach (LiveWireLateralPath p in ReWorkPaths) { // Assign patch to a worker thread workerLateralPaths[currentThreadIndex].Add(p); currentThreadIndex = (currentThreadIndex + 1) % ThreadCount; } // Dictionary of end points Dictionary <Int32Point, int> endPoints = new Dictionary <Int32Point, int>(); for (int i = 0; i < CurrentPaths.Count; i++) { LiveWirePrimaryPath currentPath = CurrentPaths[i]; foreach (Point p in currentPath.Path) { Int32Point key = (Int32Point)p; if (!endPoints.ContainsKey(key)) { endPoints.Add(key, i); } } } // Launch workers for (int worker = 0; worker < currentWorkers.Count; worker++) { LiveWireLateralWorker w = currentWorkers[worker] as LiveWireLateralWorker; w.Graph = Graph; w.Mode = LiveWireWorker.WorkMode.ReWork; w.TerminalCollection = Terminals; w.CurrentPathIndexes = endPoints; w.ReWorkPaths = workerLateralPaths[worker]; w.DistanceMap = DistanceMap; w.RunWorkerAsync(); } }