Example #1
0
        /// <summary>
        /// Starts the simulation.
        /// </summary>
        internal static void Start()
        {
            // The price table on the server has a volatile price table.  Therefore, the prices will all be zero (and any market values based off those prices will
            // be zero) until prices are loaded.  This provides the initial prices for the volatile price table.
            if (!PriceSimulator.arePricesInitialized)
            {
                PriceSimulator.InitializeCurrencyPrice();
                PriceSimulator.InitializeEquityPrice();
                PriceSimulator.InitializeDebtPrice();
                PriceSimulator.arePricesInitialized = true;
            }

            // This will start the price simulator.
            PriceSimulator.priceSimulationThread      = new Thread(new ThreadStart(PriceSimulator.SimulatePrice));
            PriceSimulator.priceSimulationThread.Name = "Price Simulator";
            PriceSimulator.priceSimulationThread.Start();
        }
        /// <summary>
        /// Called when the service is stopped.
        /// </summary>
        protected override void OnStop()
        {
            try
            {
                // This shuts down the broker simulation when it is running.
                if (MarketSimulator.SimulatorParameters.IsExchangeSimulatorRunning)
                {
                    BrokerSimulator.Stop();
                }

                // This shuts down the price simulation when it is running.
                if (MarketSimulator.SimulatorParameters.IsPriceSimulatorRunning)
                {
                    PriceSimulator.Stop();
                }

                // Shut down each of the Web Services hosted by this Windows Service.
                foreach (ServiceHost serviceHost in this.serviceHosts)
                {
                    serviceHost.Close();
                }
            }
            catch (Exception exception)
            {
                // Any problems initializing should be sent to the Event Log.
                EventLog.WriteEntry(MarketSimulator.Source, String.Format("{0}: {1}", exception.Message, exception.StackTrace), EventLogEntryType.Information);
            }

            // Log the end of the service.
            try
            {
                EventLog.WriteEntry(
                    MarketSimulator.Source,
                    String.Format(Simulator.Properties.Resources.ServiceStopping, MarketSimulator.ServiceName),
                    EventLogEntryType.Information);
            }
            catch (SecurityException)
            {
            }
        }