Beispiel #1
0
            /// <summary>Run the jobs.</summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            public void Run(object sender, System.ComponentModel.DoWorkEventArgs e)
            {
                JobManager jobManager = e.Argument as JobManager;

                List <JobManager.IRunnable> jobs = new List <JobManager.IRunnable>();

                DataStore store = Apsim.Child(simulations, typeof(DataStore)) as DataStore;

                Simulation[] simulationsToRun;
                if (model is Simulations)
                {
                    // As we are going to run all simulations, we can delete all tables in the DataStore. This
                    // will clean up order of columns in the tables and removed unused ones.
                    store.DeleteAllTables();
                    simulationsToRun = Simulations.FindAllSimulationsToRun(simulations);
                }
                else
                {
                    store.RemoveUnwantedSimulations(simulations);

                    if (model is Simulation)
                    {
                        if (model.Parent == null)
                        {
                            // model is already a cloned simulation, probably from user running a single
                            // simulation from an experiment.
                            simulationsToRun = new Simulation[1] {
                                model as Simulation
                            };
                        }
                        else
                        {
                            simulationsToRun = new Simulation[1] {
                                Apsim.Clone(model as Simulation) as Simulation
                            };
                            Simulations.CallOnLoaded(simulationsToRun[0]);
                        }
                    }
                    else
                    {
                        simulationsToRun = Simulations.FindAllSimulationsToRun(model);
                    }
                }

                store.Disconnect();

                simulations.MakeSubstitutions(simulationsToRun);

                foreach (Simulation simulation in simulationsToRun)
                {
                    jobs.Add(simulation);
                    jobManager.AddJob(simulation);
                }

                // Wait until all our jobs are all finished.
                while (AreSomeJobsRunning(jobs))
                {
                    Thread.Sleep(200);
                }

                // Collect all error messages.
                foreach (Simulation job in simulationsToRun)
                {
                    if (job.ErrorMessage != null)
                    {
                        ErrorMessage += job.ErrorMessage + Environment.NewLine;
                    }
                }

                // <summary>Call the all completed event in all models.</summary>
                object[] args = new object[] { this, new EventArgs() };
                foreach (Model childModel in Apsim.ChildrenRecursively(simulations))
                {
                    try
                    {
                        Apsim.CallEventHandler(childModel, "AllCompleted", args);
                    }
                    catch (Exception err)
                    {
                        ErrorMessage += "Error in file: " + simulations.FileName + Environment.NewLine;
                        ErrorMessage += err.ToString() + Environment.NewLine + Environment.NewLine;
                    }
                }

                if (ErrorMessage != null)
                {
                    throw new Exception(ErrorMessage);
                }
            }