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++) { LiveWirePrimaryWorker thread = new LiveWirePrimaryWorker(); 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 <LiveWirePrimaryPath> > pathLists = new List <List <LiveWirePrimaryPath> >(); for (int t = 0; t < ThreadCount; t++) { pathLists.Add(new List <LiveWirePrimaryPath>()); } int currentThreadIndex = 0; foreach (LiveWirePrimaryPath p in ReWorkPaths) { // Assign an index to a worker thread pathLists[currentThreadIndex].Add(p); currentThreadIndex = (currentThreadIndex + 1) % ThreadCount; } // Launch workers for (int worker = 0; worker < currentWorkers.Count; worker++) { LiveWirePrimaryWorker w = currentWorkers[worker] as LiveWirePrimaryWorker; w.Mode = LiveWireWorker.WorkMode.ReWork; w.Graph = Graph; w.TerminalCollection = Terminals; w.ReWorkPaths = pathLists[worker]; w.DistanceMap = DistanceMap; w.RunWorkerAsync(); } }
private void OnWorkerProgressCompleted(object sender, RunWorkerCompletedEventArgs args) { LiveWirePrimaryWorker worker = sender as LiveWirePrimaryWorker; 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 <Tuple <int, int>, List <Point> >(); this.ControlPointIndices = new Dictionary <Tuple <int, int>, List <int> >(); foreach (LiveWirePrimaryWorker w in this.currentWorkers) { foreach (KeyValuePair <Tuple <int, int>, List <Point> > kvp in w.Paths) { this.Paths.Add(kvp.Key, kvp.Value); } foreach (KeyValuePair <Tuple <int, int>, List <int> > kvp in w.ControlPointIndices) { this.ControlPointIndices.Add(kvp.Key, kvp.Value); } } this.workCompleted = true; // Clear memory this.currentWorkers = null; GC.Collect(); GC.WaitForPendingFinalizers(); base.OnProgressCompleted(this, new RunWorkerCompletedEventArgs(null, null, false)); } }
public void Run() { // 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++) { LiveWirePrimaryWorker thread = new LiveWirePrimaryWorker(); 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 <Tuple <int, int> > > workerPointPairs = new List <List <Tuple <int, int> > >(); for (int t = 0; t < ThreadCount; t++) { workerPointPairs.Add(new List <Tuple <int, int> >()); } List <Tuple <int, int> > terminalPairs = new List <Tuple <int, int> >(); foreach (RootTerminal sourceTerminal in Terminals.UnlinkedSources) { foreach (RootTerminal tipTerminal in Terminals.UnlinkedPrimaries) { terminalPairs.Add(new Tuple <int, int>(Terminals.IndexOf(sourceTerminal), Terminals.IndexOf(tipTerminal))); } } if (Terminals.TerminalLinks != null && Terminals.TerminalLinks.Count > 0) { foreach (var link in Terminals.TerminalLinks) { terminalPairs.Add(new Tuple <int, int>(Terminals.IndexOf(link.Item1), Terminals.IndexOf(link.Item2))); } } int currentThreadIndex = 0; foreach (Tuple <int, int> terminalPair in terminalPairs) { // Assign patch to a worker thread workerPointPairs[currentThreadIndex].Add(terminalPair); currentThreadIndex = (currentThreadIndex + 1) % ThreadCount; } // Launch workers for (int worker = 0; worker < currentWorkers.Count; worker++) { LiveWirePrimaryWorker w = currentWorkers[worker] as LiveWirePrimaryWorker; w.Mode = LiveWireWorker.WorkMode.Work; w.TerminalPairs = workerPointPairs[worker]; w.TerminalCollection = Terminals; w.Graph = Graph; w.DistanceMap = DistanceMap; w.RunWorkerAsync(); } }