start the simulation from code

I want to start the simulation from code.

I tried with the following code, but the simulation doesn’t start (the time stays 0:00:00). When I hover over the play button, the text is changed from “play” to “pause”, so the code does something.

var runTime = IoC.Get<ISimulationSettingViewModel>().SimulationRunTime;
IoC.Get<ISimulationService>().SwitchRunState(runTime, 0);
Can someone help me how to start the simulation from code?
Thanks in advance,
1 Like

I guess the simulation doesn’t run because you didn’t set the runTime value. Try the following. Note the last line of code is used if you do not want to set the duration of the simulation run.

        public void RunSimulation()
        {
            IoC.Get<ISimulationService>().ResetSimulation();
            // Default simulation run time is 9999 years 
            var simulationRunTime = 315328464000; // 60*60*24*365*9999 (9999 years in second)
            IoC.Get<ISimulationService>().SwitchRunState(simulationRunTime, 0);
            IoC.Get<ISimulationService>().RunSimulation();
        }

 

1 Like

Thanks for your response.

This code has the same behaviour.

Is it possible that it depends from the thread your calling the code on? In my case it is a seperate thread, maybe this causes the problem.

None of the VC .NET API is thread-safe. You should always use it from the main application thread only.

1 Like

You can run the code in the UI thread by utilizing Caliburn Micro:

Caliburn.Micro.Execute.OnUIThread(() =>
{
    //your code here
});