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(); }
public override void Start(AbstractGraphModel model, int iterations, string name) { if (CurrentExecutionStatus != ExecutionStatus.Stopped) { throw new WrongExecutionStatusException("should be stopped before new start"); } this.iterations = iterations; Assembly.AnalizeOptions = model.AnalyzeOptions; Assembly.GenerationParams = model.GenerationParamValues; Assembly.AnalyzeOptionParams = model.AnalyzeOptionsValues; Assembly.ModelType = model.GetType(); Assembly.ModelName = Assembly.ModelType.Name; Assembly.Name = name; OnExecutionStatusChange(new ExecutionStatusEventArgs(ExecutionStatus.Starting)); modelsCountInEachService = (iterations / services.Count) ; if (iterations % services.Count != 0) { modelsCountInEachService ++; } for (int i = 0; i < services.Count; i++) { int startIndex = i * modelsCountInEachService; int endIndex = (i + 1) * modelsCountInEachService; services[i].Start(model, startIndex, endIndex > iterations ? iterations : endIndex); } }