Access a C# thread-based method object with Winforms

Asked

Viewed 568 times

3

I created a thread to run a process that takes too long and makes the application process stop for a great while

private void ExeConsultaClientes()
    {
        thread = new Thread(new ThreadStart(ExeConsultaClientesst));
        thread.IsBackground = true;
        thread.Start();        
    }

The method the thread executes, is this:

private void ExeConsultaClientesst(){
using (SqlCommand cmd = Program.Conn.CreateCommand())
            {
                cmd.CommandTimeout = 0;
                Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
                string sql = @" *CONSULTA*  ";
                cmd.CommandText = sql;
                DataSet tabela = new DataSet();
                SqlDataAdapter adp = new SqlDataAdapter(cmd);                                       
                adp.Fill(tabela, "Dados");                   
                *GRIDVIEW*.DataSource = tabela;
                *GRIDVIEW*.DataMember = 
                tabela.Tables[0].TableName.ToString();
                *GRIDVIEW*.Refresh();
                Cursor.Current = System.Windows.Forms.Cursors.Default;
                adp.Dispose();
                Program.Conn.Close();
                me.FlushMemory();
                filterColor();
                thread.Suspend();
            }
  }

When will he access these guys

CubCadastroPivot.DataSource = tabela;
                CubCadastroPivot.DataMember = tabela.Tables[0].TableName.ToString();
                CubCadastroPivot.Refresh();
                Cursor.Current = System.Windows.Forms.Cursors.Default;
                adp.Dispose();
                Program.Conn.Close();
                me.FlushMemory();
                filterColor();

It returns the error

Invalid threading operation: control 'GRIDVIEW' accessed from a thread that is not the one it was created on.

There is how to operate with these guys who give error in a method the part accessing the local variables of the method Execonsultclientesst ?

1 answer

2


You can use the control Invoke method to do this:

            cmd.CommandTimeout = 0;
            Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
            string sql = @" *CONSULTA*  ";
            cmd.CommandText = sql;
            DataSet tabela = new DataSet();
            SqlDataAdapter adp = new SqlDataAdapter(cmd);                                       
            adp.Fill(tabela, "Dados");     
            *GRIDVIEW*.Invoke((MethodInvoker) delegate {

                *GRIDVIEW*.DataSource = tabela.Tables[0];
                *GRIDVIEW*.Refresh();
            });
            Cursor.Current = System.Windows.Forms.Cursors.Default;
            adp.Dispose();
            Program.Conn.Close();
            me.FlushMemory();
            filterColor();
            thread.Suspend();

According to the documentation, the method Invoke:

Executes the specified delegate, in the thread that has the identifier of the control’s underlying window, with the specified argument list.

  • 1

    Very correct my good. For some time I’ve been trying to learn, but I didn’t see valid examples for my case. Thankful, hugs !

  • for nothing! arrange

Browser other questions tagged

You are not signed in. Login or sign up in order to post.