public void Start(AbstractGraphModel origineModel, int startIndex, int endIndex) { context = OperationContext.Current; context.Channel.OperationTimeout = TimeSpan.FromSeconds(1000); this.startIndex = startIndex; this.endIndex = endIndex; Models = new Dictionary<int, AbstractGraphModel>(); Threads = new Dictionary<int, Thread>(); int processorcount = Environment.ProcessorCount; for (int i = startIndex; i < endIndex; i++) { AbstractGraphModel model = origineModel.Clone(); model.Progress += new GraphProgressEventHandler(OnSeparateModelProgress); Models.Add(i, model); Threads[i] = new Thread(new ParameterizedThreadStart(StartAnalyze)) { Priority = ThreadPriority.Lowest }; } for (int i = startIndex; i < endIndex; i++) { if (Models[i].CurrentStatus.GraphProgress != GraphProgress.Stopped) { Threads[i].Start(i); if ((i + 1) % processorcount == 0) { if ((i + 1) % processorcount == 0) { for (int j = 0; j < processorcount; ++j) Threads[i - j].Join(); } } } } }
public override void Start(AbstractGraphModel origineModel, int iterations, string name) { log.Info("Started multi threaded calculation"); if (CurrentExecutionStatus != ExecutionStatus.Stopped) { log.Info("Wrong Execution Status, calculation ended"); log.Fatal("WrongExecutionStatusException throwed"); throw new WrongExecutionStatusException("should be stopped before new start"); } this.iterations = iterations; //Set permanent status origineModel.PermanentStatus = true; Assembly.AnalizeOptions = origineModel.AnalyzeOptions; Assembly.GenerationParams = origineModel.GenerationParamValues; Assembly.AnalyzeOptionParams = origineModel.AnalyzeOptionsValues; Assembly.ModelType = origineModel.GetType(); Assembly.Name = name; OnExecutionStatusChange(new ExecutionStatusEventArgs(ExecutionStatus.Starting)); waitHandles = new AutoResetEvent[iterations]; Models = new List<AbstractGraphModel>(iterations); threads = new Thread[iterations]; int processorcount = Environment.ProcessorCount; log.Info("Started creating thread for each instance"); for (int i = 0; i < iterations; i++) { AbstractGraphModel model = origineModel.Clone(); model.SetID(i); model.Progress += new GraphProgressEventHandler(OnSeparateModelProgress); model.GraphGenerated += new CommonLibrary.Model.Events.GraphGeneratedDelegate(model_GraphGenerated); Models.Add(model); waitHandles[i] = new AutoResetEvent(false); threads[i] = new Thread(new ParameterizedThreadStart(StartAnalyze)) { Priority = ThreadPriority.Lowest }; } log.Info("Ended creating thread for each instance"); for (int i = 0; i < iterations; i++) { if (Models[i].CurrentStatus.GraphProgress != GraphProgress.Stopped) { log.Info("Threads are starting"); threads[i].Start(i); if ((i + 1) % processorcount == 0) { log.Info("Threads are joined"); for (int j = 0; j < processorcount; ++j) threads[i - j].Join(); } } } Thread waitingThread = new Thread(new ThreadStart(Waiting)); waitingThread.Start(); }