Click here to Skip to main content
15,887,428 members
Home / Discussions / C#
   

C#

 
GeneralRe: Override properties in derived classes - some have fixed value, some do not Pin
OriginalGriff2-Sep-16 3:57
mveOriginalGriff2-Sep-16 3:57 
QuestionTimeStamp to PDF Pin
Member 119141971-Sep-16 22:06
Member 119141971-Sep-16 22:06 
QuestionSuggesstions on saving 100s of rowsto the database Pin
Giridhar Aruri1-Sep-16 15:27
Giridhar Aruri1-Sep-16 15:27 
AnswerRe: Suggesstions on saving 100s of rowsto the database Pin
OriginalGriff1-Sep-16 20:22
mveOriginalGriff1-Sep-16 20:22 
AnswerRe: Suggesstions on saving 100s of rowsto the database Pin
Eddy Vluggen1-Sep-16 23:10
professionalEddy Vluggen1-Sep-16 23:10 
QuestionNeed to cancel BackgroundWorker Pin
David_411-Sep-16 13:01
David_411-Sep-16 13:01 
AnswerRe: Need to cancel BackgroundWorker Pin
Pete O'Hanlon1-Sep-16 21:01
mvePete O'Hanlon1-Sep-16 21:01 
GeneralRe: Need to cancel BackgroundWorker Pin
David_416-Sep-16 8:12
David_416-Sep-16 8:12 
Thank you Peter. I actually worked on this over my long weekend. Sorry for not getting back to you sooner.

I got it working by partly using your suggestion to abort the thread. This works because the only types of queries that will be run here are SELECT queries. So no records are actually being modified. So, no need to worry about rolling back transactions.

I am no longer using a background worker. I am just creating my own thread. I want to show my code below.

Class Level Code:
C#
CloseCancelForm(); //This form contains the Cancel Button


Cancel Form
C#
public partial class FormCancelQuery : Form
    {
        DialogResult result = DialogResult.OK;
        public FormCancelQuery()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            result = DialogResult.Cancel;
            this.Close();
        }
    }


Main UI Form
C#
private void qbTestQueryBtn_Click(object sender, EventArgs e)
        {            
            frmCancel = new FormCancelQuery();
            RunQueryThread = new Thread(new ThreadStart(RunQuery));
            RunQueryThread.Start();
            DialogResult dr = frmCancel.ShowDialog(this);
            if (dr == DialogResult.Cancel)
            {
                frmCancel.DialogResult = DialogResult.OK;
                RunQueryThread.Abort();
            }                       
        }

private void RunQuery()
        {
            if (ValidQuery())
            {
                string queryTestString = GlobalStr.previewQueryString;
                // Check the query for the where fill flag, and if it contains it, branch off to the filling routine
                string fillFlag = "'mandafillflag'";
                if (queryTestString.ToLower().Contains(fillFlag))
                {
                    MessageBox.Show("This query requires completing the fields. Click OK to begin procedure.");
                    QueryFiller qf = new QueryFiller();
                    queryTestString = qf.FillQuery(queryTestString.ToLower(), fillFlag);
                }

                GlobalStr.previewQueryString = queryTestString;
                //selectTB.Text = GlobalStr.previewQueryString;
                selectTB.Invoke(new MethodInvoker(delegate { selectTB.Text = GlobalStr.previewQueryString; }));
                //whereTB.Clear();
                whereTB.Invoke(new MethodInvoker(delegate { whereTB.Clear(); }));
                //fromTB.Clear();
                fromTB.Invoke(new MethodInvoker(delegate { fromTB.Clear(); }));
                //orderTB.Clear();
                orderTB.Invoke(new MethodInvoker(delegate { orderTB.Clear(); }));

                try
                {
                    //examineQueryInputString(GlobalStr.previewQueryString, "", cmplxWhereClauseCB.Checked);
                    examineQueryInputString(GlobalStr.previewQueryString, "", GlobalBool.g_CmplxWhereClauseChecked);
                }
                catch (System.Exception ex)
                {
                    CloseCancelForm();
                    MessageBox.Show(ex.Message);
                    return;
                }

                // Clear the data table
                
                try
                {
                    GlobalData.g_queryData.Rows.Clear();
                    GlobalData.g_queryData.Columns.Clear();
                }
                catch (Exception ex)
                {
                    GlobalData.g_queryData = new DataTable();                    
                }                
                
                // set the wait cursor if query takes long. ens 01/28/2015
                Cursor.Current = Cursors.WaitCursor;
                //using (SqlConnection connection = new SqlConnection(GlobalStr.sqlConnString))
                using (SqlConnection connection = new SqlConnection(GlobalStr.sqlConnStringMetadata))
                {
                    try
                    {
                        connection.Open();

                        /*
                         * DRW 3/10/2016
                         * Added the following Transaction object in order to set the isolation level of all queries
                         * run from MAnDA to to Read Uncommited.
                         */
                        SqlTransaction Transaction = connection.BeginTransaction(IsolationLevel.ReadUncommitted);

                        using (SqlCommand testQuery = new SqlCommand(GlobalStr.previewQueryString, connection))
                        {
                            /*
                             * DRW 3/11/2016
                             * User sets the query timeout value via a textbox on the main form. The value is held in a variable.
                             * testQuery.CommandTimeout = 300;
                             */

                            testQuery.CommandTimeout = m_QueryTimeout;
                            testQuery.Transaction = Transaction;                           

                            using (SqlDataAdapter adapter3 = new SqlDataAdapter(testQuery))
                            {
                                adapter3.Fill(GlobalData.g_queryData);

                                // Open the DataSetViewer form and send the table to it
                                DataSetViewer dsv = new DataSetViewer();
                                dsv.SetSource(GlobalData.g_queryData);
                                // set the dataview classification 05272015
                                string strClassification = string.Format("{0} {1}", GlobalStr.g_classification, GlobalStr.g_releaseability);
                                dsv.setClassification(strClassification);
                                Cursor.Current = Cursors.Default;
                                CloseCancelForm();
                                dsv.ShowDialog();

                                //dataGridView1.DataSource = GlobalData.g_queryData;
                            }
                        }
                        connection.Close();                        
                    }
                    catch (Exception ex)
                    {
                        //frmCancel.Invoke(new MethodInvoker(delegate { frmCancel.Close(); }));                        
                        if (ex.Message != "Thread was being aborted.")
                        {
                            CloseCancelForm();
                            /// Print error message
                            MessageBox.Show("The query test failed: " + ex.Message);                            
                        }
                        return;
                    }
                }
            }
            else
            {
                CloseCancelForm();
                MessageBox.Show("SQL test failed.");
            } 
        }

        private void CloseCancelForm()
        {            
            try
            {
                if (frmCancel.InvokeRequired)
                {
                    frmCancel.Invoke(new MethodInvoker(delegate { frmCancel.DialogResult = DialogResult.OK; }));
                    frmCancel.Invoke(new MethodInvoker(delegate { frmCancel.Close(); }));
                }
                else
                {
                    frmCancel.DialogResult = DialogResult.OK;
                    frmCancel.Close();
                }                
            }
            catch(Exception ex)
            {
                string message = ex.Message;
                return;
            }
        }


Hopefully this will help someone.

David
GeneralRe: Need to cancel BackgroundWorker Pin
Pete O'Hanlon6-Sep-16 9:39
mvePete O'Hanlon6-Sep-16 9:39 
AnswerRe: Need to cancel BackgroundWorker Pin
Gerry Schmitz3-Sep-16 6:53
mveGerry Schmitz3-Sep-16 6:53 
GeneralRe: Need to cancel BackgroundWorker Pin
David_416-Sep-16 8:20
David_416-Sep-16 8:20 
GeneralRe: Need to cancel BackgroundWorker Pin
Gerry Schmitz6-Sep-16 9:08
mveGerry Schmitz6-Sep-16 9:08 
GeneralRe: Need to cancel BackgroundWorker Pin
David_416-Sep-16 9:14
David_416-Sep-16 9:14 
GeneralRe: Need to cancel BackgroundWorker Pin
David_416-Sep-16 9:18
David_416-Sep-16 9:18 
GeneralRe: Need to cancel BackgroundWorker Pin
Gerry Schmitz6-Sep-16 9:48
mveGerry Schmitz6-Sep-16 9:48 
GeneralRe: Need to cancel BackgroundWorker Pin
David_416-Sep-16 10:05
David_416-Sep-16 10:05 
GeneralRe: Need to cancel BackgroundWorker Pin
Gerry Schmitz6-Sep-16 10:25
mveGerry Schmitz6-Sep-16 10:25 
QuestionFacebook login form Pin
Launchar31-Aug-16 18:14
Launchar31-Aug-16 18:14 
AnswerRe: Facebook login form Pin
Mycroft Holmes31-Aug-16 19:38
professionalMycroft Holmes31-Aug-16 19:38 
AnswerRe: Facebook login form Pin
Nathan Minier1-Sep-16 0:59
professionalNathan Minier1-Sep-16 0:59 
QuestionPRINT DATE TIME & TEXT ON PICTUREBOX Pin
Member 1271206730-Aug-16 18:11
Member 1271206730-Aug-16 18:11 
AnswerRe: PRINT DATE TIME & TEXT ON PICTUREBOX Pin
Mycroft Holmes30-Aug-16 20:17
professionalMycroft Holmes30-Aug-16 20:17 
QuestionGet value from string and pass it to another field Pin
Member 1105143930-Aug-16 11:50
Member 1105143930-Aug-16 11:50 
AnswerRe: Get value from string and pass it to another field Pin
Simon_Whale30-Aug-16 13:11
Simon_Whale30-Aug-16 13:11 
AnswerRe: Get value from string and pass it to another field Pin
Dave Kreskowiak30-Aug-16 13:17
mveDave Kreskowiak30-Aug-16 13:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.