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?