private void CancelQueryButton_Click(object sender, EventArgs e)
        {
            QueryExecutionEngine executionEngine = _currentExecutionEngine;

            if (executionEngine != null)
            {
                executionEngine.CancelQuery();
            }
        }
        private void OnExecuteComplete(object sender, SqlExecuteCompleteEventArgs e)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action <object, SqlExecuteCompleteEventArgs>(DisplayResults), sender, e);
            }
            else
            {
                DisplayResults(sender, e);
            }

            _currentExecutionEngine = null;
            UpdateDatabaseContext();
        }
        private void Execute()
        {
            _messagesTab                = new TabPage(_tabTitleMessages);
            _messagesTextBox            = new RichTextBox();
            _messagesTextBox.Anchor     = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;
            _messagesTextBox.Size       = _messagesTab.Size;
            _messagesTextBox.WordWrap   = false;
            _messagesTextBox.Multiline  = true;
            _messagesTextBox.ScrollBars = RichTextBoxScrollBars.Both;
            _messagesTextBox.Font       = new Font("Consolas", 10);
            _messagesTab.Controls.Add(_messagesTextBox);

            try
            {
                if (_connection == null ||
                    _connection.IsAvailable == false)
                {
                    if (ConnectDatabase() == false)
                    {
                        return;
                    }
                }

                string sqlToExecute = queryRichTextBox.SelectedText;
                if (string.IsNullOrEmpty(sqlToExecute))
                {
                    sqlToExecute = queryRichTextBox.Text;
                }

                ViewerSettings.Instance.CurrentQuery    = queryRichTextBox.Text;
                ViewerSettings.Instance.CurrentQueryRtf = queryRichTextBox.Rtf;
                ViewerSettings.Instance.Save();

                // Clear all but the script tab
                List <TabPage> tabsToDelete = new List <TabPage>();
                foreach (TabPage tab in mainTabControl.TabPages)
                {
                    if (tab.Text != _tabTitleScript)
                    {
                        tabsToDelete.Add(tab);
                    }
                }
                tabsToDelete.ForEach(t => mainTabControl.TabPages.Remove(t));

                if (ViewerSettings.Instance.TraceFlagListHasBeenEdited)
                {
                    SetTraceFlags();
                }

                this.executeButton.Enabled               = false;
                this.connectButton.Enabled               = false;
                this.cancelQueryButton.Enabled           = true;
                this.disconnectButton.Enabled            = false;
                this.disconnectToolStripMenuItem.Enabled = false;
                this.executionStatus.Text = "Executing query";

                _executionTimer          = new Timer();
                _executionTimer.Interval = 50;
                _executionTimer.Tick    += ExecutionTimer_Tick;
                _executionStartTime      = DateTime.Now;
                _executionTimer.Enabled  = true;

                _currentExecutionEngine = new QueryExecutionEngine(_connection, sqlToExecute);
                _currentExecutionEngine.ExecuteComplete += OnExecuteComplete;
                _currentExecutionEngine.StartExecution();
            }
            catch (Exception ex)
            {
                DisplayException(ex);
            }
        }