protected void invokeOverallProgress(AbstractGraphModel graphModel)
 {
     OnOverallProgress(new GraphProgressEventArgs(graphModel.CurrentStatus));
 }
        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();
        }
 public abstract void Start(AbstractGraphModel model, int iterations, string name);
 public void ProgressReport(AbstractGraphModel model, GraphProgressEventArgs args)
 {
     manager.OnSeparateModelProgress(model, args);
 }
 public IAsyncResult BeginProgressReport(AbstractGraphModel model, GraphProgressEventArgs args, AsyncCallback callback, object asyncState)
 {
     throw new NotImplementedException();
 }
        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);
            }
        }
 public void OnSeparateModelProgress(AbstractGraphModel model, GraphProgressEventArgs args)
 {
     invokeOverallProgress(model);
     if (args.Progress.GraphProgress == GraphProgress.Done)
     {
         Assembly.Results.Add(model.Result);
         if (Assembly.Results.Count == iterations)
         {
             OnExecutionStatusChange(new ExecutionStatusEventArgs(ExecutionStatus.Success));
         }
     }
     else if (args.Progress.GraphProgress == GraphProgress.Failed)
     {
         OnExecutionStatusChange(new ExecutionStatusEventArgs(ExecutionStatus.Failed));
     }
 }
 public void Start(AbstractGraphModel model, int startIndex, int endIndex)
 {
     service.Start(model, startIndex, endIndex);
 }