private static void RunApplication(object data) { try { bool verboseOff = (bool)((object[])data)[0]; string oprFilename = (string)((object[])data)[1]; // check whether opr file exists if (oprFilename == null) { Console.WriteLine("Error: -r switch was not specified."); _exitCode = 3; return; } FileInfo fileInfo = new FileInfo(oprFilename); if (!fileInfo.Exists) { Console.WriteLine("Error: cannot find input file " + oprFilename); _exitCode = 4; return; } // open OPR CompositionManager composition = new CompositionManager(); if (!verboseOff) { Console.WriteLine("Loading project file " + fileInfo.FullName + "..."); } composition.LoadFromFile(fileInfo.FullName); // prepare listeners if (!verboseOff) { Console.WriteLine("Preparing listener(s)..."); } ArrayList listOfListeners = new ArrayList(); // logfile listener if (composition.LogToFile != null && composition.LogToFile != "") { // get composition file's directory to logfile is saved in same directory string logFileName = Utils.GetFileInfo(fileInfo.DirectoryName, composition.LogToFile).FullName; LogFileListener logFileListener = new LogFileListener(composition.ListenedEventTypes, logFileName); listOfListeners.Add(logFileListener); } // console listener if (!verboseOff) { ConsoleListener consoleListener = new ConsoleListener(composition.ListenedEventTypes); listOfListeners.Add(consoleListener); } // create proxy listener ProxyListener proxyListener = new ProxyListener(); proxyListener.Initialize(listOfListeners); // run simulation if (!verboseOff) { Console.WriteLine("Starting composition run..."); } composition.Run(proxyListener, true); if (!verboseOff) { Console.WriteLine("Closing composition..."); } composition.Release(); _exitCode = 0; } catch (Exception e) { Console.WriteLine("Exception occured: " + e.ToString()); _exitCode = -2; return; } }
static void Main() { Config = JsonConvert.DeserializeObject <Config>(File.ReadAllText("Config.json")); if (CfxRuntime.PlatformArch == CfxPlatformArch.x64) { CfxRuntime.LibCefDirPath = Path.GetFullPath(@"..\..\..\libcef\x64"); } else { CfxRuntime.LibCefDirPath = System.IO.Path.GetFullPath(@"..\..\..\libcef\x86"); } CfxRuntime.LibCfxDirPath = CfxRuntime.LibCefDirPath; string assemblyDir = System.IO.Path.GetDirectoryName( new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath ); //CfxRuntime.EnableHighDpiSupport(); int exitCode = CfxRuntime.ExecuteProcess(null); if (exitCode >= 0) { Environment.Exit(exitCode); } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); var settings = new CfxSettings(); settings.WindowlessRenderingEnabled = true; settings.NoSandbox = true; //settings.SingleProcess = true; settings.BrowserSubprocessPath = Path.Combine(assemblyDir, "xRemote"); //settings.LogSeverity = CfxLogSeverity.Disable; settings.ResourcesDirPath = Path.GetFullPath(@"..\..\..\libcef\Resources"); settings.LocalesDirPath = Path.GetFullPath(@"..\..\..\libcef\Resources\locales"); var app = new CfxApp(); app.OnBeforeCommandLineProcessing += (s, e) => { e.CommandLine.AppendSwitch("disable-web-security"); e.CommandLine.AppendSwitch("enable-system-flash"); // optimizations following recommendations from issue #84 //e.CommandLine.AppendSwitch("disable-gpu"); //e.CommandLine.AppendSwitch("disable-gpu-compositing"); //e.CommandLine.AppendSwitch("disable-gpu-vsync"); }; if (!CfxRuntime.Initialize(settings, app)) { Environment.Exit(-1); } Common.GlobalObjectPool.InitializePool(); // 监听连接请求 ProxyListener.Initialize(); var f = new MainForm(); f.Show(); Application.Idle += (s, e) => CfxRuntime.DoMessageLoopWork(); Application.Run(f); Common.GlobalObjectPool.DestroyPool(); CfxRuntime.Shutdown(); }
/// <summary> /// Runs simulation. /// </summary> /// <param name="runListener">Simulation listener.</param> /// <param name="runInSameThread">If <c>true</c>, simulation is run in same thread like caller, /// ie. method blocks until simulation don't finish. If <c>false</c>, simulation is /// run in separate thread and method returns immediately.</param> /// <remarks> /// Simulation is run the way that trigger invokes <see cref="ILinkableComponent.GetValues">ILinkableComponent.GetValues</see> /// method of the model it's connected to /// at the time specified by <see cref="TriggerInvokeTime">TriggerInvokeTime</see> property. /// If you need to use more than one listener you can use <see cref="ProxyListener">ProxyListener</see> /// class or <see cref="ProxyMultiThreadListener">ProxyMultiThreadListener</see> if <c>runInSameThread</c> is <c>false</c>. /// </remarks> public void Run(Logger logger, bool runInSameThread) { LoggerListener loggerListener = new LoggerListener(logger); ProgressListener progressListener = new ProgressListener(); progressListener.ModelProgressChangedHandler += new ModelProgressChangedDelegate(delegate(ILinkableComponent linkableComponent, ITimeStamp simTime) { string guid = cmGuidMapping[linkableComponent]; string progress = CalendarConverter.ModifiedJulian2Gregorian(simTime.ModifiedJulianDay).ToString(); CompositionModelProgressChangedHandler(this, guid, progress); }); ArrayList listeners = new ArrayList(); listeners.Add(loggerListener); listeners.Add(progressListener); ProxyListener proxyListener = new ProxyListener(); proxyListener.Initialize(listeners); startTime = DateTime.Now; if (_running) throw (new Exception("Simulation is already running.")); _running = true; _runListener = proxyListener; try { TimeStamp runToTime = new TimeStamp(CalendarConverter.Gregorian2ModifiedJulian(_triggerInvokeTime)); // Create informative message if (_runListener != null) { StringBuilder description = new StringBuilder(); description.Append("Starting simulation at "); description.Append(DateTime.Now.ToString()); description.Append(","); description.Append(" composition consists from following models:\n"); foreach (Model model in _models) { description.Append(model.ModelID); description.Append(", "); } // todo: add more info? Event theEvent = new Event(EventType.Informative); theEvent.Description = description.ToString(); _runListener.OnEvent(theEvent); } _runPrepareForComputationStarted = true; // prepare for computation if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Preparing for computation...."; _runListener.OnEvent(theEvent); } // subscribing event listener to all models if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Subscribing proxy event listener...."; _runListener.OnEvent(theEvent); for (int i = 0; i < _runListener.GetAcceptedEventTypeCount(); i++) foreach (Model uimodel in _models) { theEvent = new Event(EventType.Informative); theEvent.Description = "Calling Subscribe() method with EventType." + ((EventType)i).ToString() + " of model " + uimodel.ModelID; _runListener.OnEvent(theEvent); for (int j = 0; j < uimodel.LinkableComponent.GetPublishedEventTypeCount(); j++) if (uimodel.LinkableComponent.GetPublishedEventType(j) == _runListener.GetAcceptedEventType(i)) { uimodel.LinkableComponent.Subscribe(_runListener, _runListener.GetAcceptedEventType(i)); break; } } } if (!runInSameThread) { // creating run thread if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Creating run thread...."; _runListener.OnEvent(theEvent); } _runThread = new Thread(RunThreadFunction) { Priority = ThreadPriority.Normal }; // starting thread... if (_runListener != null) { Event theEvent = new Event(EventType.GlobalProgress); theEvent.Description = "Starting run thread...."; _runListener.OnEvent(theEvent); } _runThread.Start(); } else { // run simulation in same thread (for example when running from console) if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Running simulation in same thread...."; _runListener.OnEvent(theEvent); } RunThreadFunction(); } } catch (System.Exception e) { if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Exception occured while initiating simulation run: " + e.ToString(); _runListener.OnEvent(theEvent); _runListener.OnEvent(SimulationFailedEvent); // todo: add info about time to this event endTime = DateTime.Now; var ev = new Event { Description = "Elapsed time: " + (endTime - startTime), Type = EventType.GlobalProgress }; _runListener.OnEvent(ev); } } finally { } }