我的问题很简单,我的SQL查询需要2分钟才能完成,我不能让我的应用程序在尝试获取所有数据时被冻结。我尝试了多线程,但是我一直在遇到一个我很确定你会认识的错误。我的代码就在这个下面。在线程中加载数据
跨线程操作无效:从其创建的线程以外的线程访问的控件'labelEdit1'。
private void Form_Load(object sender, EventArgs e)
{
startup = new Thread(loadInThread);
startup.Start();
}
private void loadInThread()
{
//getsDataFromSQL() is the method that takes over 2 minutes to do
//it returns a String Array if that is helpful
comboEdit1.Properties.Items.AddRange(getsDataFromSQL());
startup.Abort();
}
如果有更好的方法来做到这一点,那么请让我知道,我需要的是为应用程序不会冻结起来,这些数据取得装入comboEdit。我也知道SQL语句可以优化,但这不是这个问题的关键,所以请不要提示它。
您是否对交叉线程异常做过任何研究?这个问题的第一个相关问题有标题“跨线程操作无效:控制访问从一个线程以外的线程创建它”... –
http://stackoverflow.com/questions/142003/cross -thread-operation-not-valid-control-accessible-from-a-thread-other-than-the-rq = 1 –
-1缺少研究 –