protected void Application_End(Object sender, EventArgs e)
        {
            // -- run all OnApplicationStart background tasks
            CmsBackgroundTaskUtils.RunAllApplicationEndBackgroundTasks();

            // -- get rid of the periodic background events timer.
            if (_timer != null)
            {
                _timer.Dispose();
            }

            Console.WriteLine("Application_End");
        }
        protected void Application_Start(Object sender, EventArgs e)
        {
            // -- initialize the console trace listener
            _traceListener = new ConsoleTraceListener();
            System.Diagnostics.Trace.Listeners.Add(_traceListener);

            // -- run all OnApplicationStart background tasks
            CmsBackgroundTaskUtils.RunAllApplicationStartBackgroundTasks();

            // -- initialize Period Background Task timer
            int dueTime_ms = 60 * 1000; // milliseconds to wait before calling RunBackgroundPeriodicTasks for first time.

            _timer = new System.Threading.Timer(RunBackgroundPeriodicTasks, null, dueTime_ms, _backgroundTimerPeriod_ms);
        }
        private static void RunBackgroundPeriodicTasks(object state)
        {
            // -- ensure we don't call this function again until RunBackgroundTasks has returned
            _timer.Change(Timeout.Infinite, Timeout.Infinite);
            try
            {
                CmsBackgroundTaskUtils.RunAllApplicablePeriodicTasks();
            }
            catch (Exception ex)
            {
                Console.Write("Background Task Error: Something went wrong. Reason: {0} Stack: {1}", ex.Message, ex.StackTrace);
            }


            _timer.Change(_backgroundTimerPeriod_ms, _backgroundTimerPeriod_ms);
        }