public WebHawkAppContext(bool startMinimized)
        {
            //Initialize Controller & Scheduler
            zCheckIEBrowserMode();

            //Instantiate m_MainForm here. This sets the SynchronizationContext.Current value needed to start the scheduler.
            //When the scheduler is split out of this executable process (to a service or agent application), we won't need to worry about this.
            m_MainForm    = new frmMain();
            this.MainForm = m_MainForm;

            string connectionString = ConfigurationManager.ConnectionStrings["WebHawkDb"].ConnectionString;

            AutomationController = new AutomationController(connectionString);
            SettingsController   = new SettingsController(connectionString);
            SchedulingController = new scheduling.SchedulingController(connectionString);
            Scheduler            = new scheduling.Scheduler(connectionString, new ThreadSynchronizer());
            Scheduler.Start();

            //Set up tray icon
            m_Notify                   = new NotifyIcon();
            m_Notify.Icon              = (System.Drawing.Icon)Properties.Resources.ResourceManager.GetObject("Icon1");
            m_Notify.Text              = "Double-click to open.";
            m_Notify.MouseDoubleClick += new MouseEventHandler(m_Notify_MouseDoubleClick);
            m_Notify.Visible           = true;

            //Show frmMain
            if (startMinimized)
            {
                m_MainForm.WindowState = FormWindowState.Minimized;
            }
            else
            {
                m_MainForm.Show();
            }
        }
        public void Execute(IJobExecutionContext context)
        {
            ThreadSynchronizer threadSynchronizer = null;
            frmAutomation      frmAutomation      = null;

            try
            {
                m_JobKey = context.JobDetail.Key;
                m_Logger.InfoFormat("ExecuteSequenceJob {0} executing at {1}.", m_JobKey, DateTime.Now.ToString("r"));

                string connectionString = (string)context.MergedJobDataMap["connectionString"];
                AutomationController automationController = new AutomationController(connectionString);
                SchedulingController schedulingController = new SchedulingController(connectionString);

                long           scheduledTaskId = (long)context.MergedJobDataMap["scheduledTaskId"];
                ScheduledTask  scheduledTask   = schedulingController.GetScheduledTask(scheduledTaskId, DeletedInclusion.All);
                SequenceDetail sequenceDetail  = automationController.GetSequenceDetail(scheduledTask.TaskSequence.SequenceId);

                threadSynchronizer = (ThreadSynchronizer)context.MergedJobDataMap["threadSynchronizer"];
                threadSynchronizer.RunOnSynchronizedThread(() => frmAutomation = new frmAutomation(), true);

                using (AutomationEngine automationEngine = new AutomationEngine(frmAutomation.Browser))
                {
                    automationEngine.SetSequence(sequenceDetail.SequenceSteps);
                    Dictionary <string, IStateVariable> persistedData = automationController.GetSequencePersistedData(sequenceDetail.Sequence.SequenceId);
                    automationEngine.DataContext.LoadPersistedVariables(persistedData);

                    automationEngine.ExecutionComplete += automationEngine_ExecutionComplete;
                    using (m_ExecutionWaitHandle = new AutoResetEvent(false))
                    {
                        automationEngine.ExecuteSequence();

                        if (!m_ExecutionWaitHandle.WaitOne(scheduledTask.RunDurationLimit.HasValue
                            ? scheduledTask.RunDurationLimit.Value
                            : TimeSpan.FromMilliseconds(-1)))
                        {
                            throw new TimeoutException(
                                      String.Format("Scheduled sequence execution timed out because it reached the specified Run Duration Limit of {0}.",
                                                    scheduledTask.RunDurationLimit));
                        }
                    }
                    automationEngine.ExecutionComplete -= automationEngine_ExecutionComplete;

                    persistedData = automationEngine.DataContext.GetPersistedVariables();
                    automationController.SetSequencePersistedData(sequenceDetail.Sequence.SequenceId, persistedData);
                }

                ExecuteSequenceJobResult result = new ExecuteSequenceJobResult(
                    zGetScheduledTaskStatusFromExecutionState(m_ExecutionCompleteEventArgs.RunResult),
                    "");     //TODO: get error from ExecutionCompleteEventArgs
                context.Result = result;
            }
            catch (Exception ex)
            {
                JobExecutionException jobEx = new JobExecutionException(ex, false);
                throw jobEx;
            }
            finally
            {
                //Clean up
                try
                {
                    if (frmAutomation != null)
                    {
                        threadSynchronizer.RunOnSynchronizedThread(() => frmAutomation.Dispose(), true);
                    }
                }
                catch (Exception ex)
                {
                    if (m_Logger != null)
                    {
                        m_Logger.ErrorFormat("Cleanup failed for ExecuteSequenceJob {0}: {1}", m_JobKey, ex.Message);
                    }
                }

                //Dereference global variables
                m_Logger = null;
                m_JobKey = null;
                m_ExecutionCompleteEventArgs = null;
                m_ExecutionWaitHandle        = null;
            }
        }