我如何通过线程访问控制 [英] How can i access to control througth threads
本文介绍了我如何通过线程访问控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
我有一个需要时间执行的大查询。
当我在另一个线程中执行它时,我有一个问题。
让我们看看
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
// pseudocode
Thread startSql=new Thread(new ThreadStart(RunSql));
public void RunSql()
{
DataGridView1.datasource=Connection.runselect(sqlStatment);
}
//where runselect return table
当我尝试运行线程时,它抛出异常,我无法通过另一个线程访问 datagridview
。
如何解决这个问题?
谢谢。
When I try to run thread it throw exception that I cannot access the datagridview
through another thread.
How can I solve this?
Thanks.
推荐答案
你可以'除了从创建它们的线程(UI线程)访问控件。
为了做到这一点,你必须使用P / Invoke:
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:
Invoke(new MethodInvoker(delegate { DataGridView1.datasource=Connection.runselect(sqlStatment); }));
这篇关于我如何通过线程访问控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文