Access object of a method executed by thread in the background in C # with WinForms

3

I created a thread to execute a process that takes a lot of time and makes the application process stop for a very long time

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 it will access these faces

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 thread operation: control ' GRIDVIEW ' accessed from a thread that is not the one in which it was created.

Is there a way to work with these guys that give error in a method to part by accessing the local variables of the ExeConsultaClientesst method?

    
asked by anonymous 09.03.2018 / 15:02

1 answer

2

You can use the Invoke method of the control 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 Invoke method:

  

Executes the specified delegate, on the thread that has the handle of the underlying window of the control, with the list of arguments specified.

    
09.03.2018 / 15:18