Click here to Skip to main content
15,897,291 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a big query that takes time to execute.
When I execute it in another thread I have a problem .

Let us see
C#
// pseudocode
  Thread startSql=new Thread(new ThreadStart(RunSql));
  public void RunSql()
    {
       DataGridView1.datasource=Connection.runselect(sqlStatment);
    } 
//where runselect return table

When I try to run thread it throw exception that I cannot access the datagridview through another thread.

How can I solve this?

Thanks.
Posted
Updated 11-May-13 22:39pm
v3

1 solution

You can't access controls except from the thread on which they were created (the UI thread).
In order to do it you must use P/Invoke:
C#
Invoke(new MethodInvoker(delegate { DataGridView1.datasource=Connection.runselect(sqlStatment); }));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900