public void StartRun()
{
//this.GetType().GetMethod("running").Invoke(this,null);
// //myExcel.GetType().GetMethod("MakeExcelFile").Invoke(myExcel , arg);
Thread mythread=new Thread(new ThreadStart(this.running));
mythread.Start();
} public void running()
{
try
{
string tempsql="SELECT convert(int,djzh) from cw_dalr where convert(int,djzh)>={0} and convert(int,djzh)<={1} order by convert(int,djzh)";
this.label1.Text="正在分析数据...";
int from=int.Parse(tb_djzh1.Text);
int to=int.Parse(tb_djzh.Text);
string sql=string.Format(tempsql,from.ToString(),to.ToString());
DataSet myds=cSqlHelper.ExecuteDataset(cSqlHelper.conn,CommandType.Text,sql);
Hashtable myhash=new Hashtable();
string howmany="";
int temp=0;
int tempfrom=0,tempto=0;
//填充hashtable


for(int i=0;i<myds.Tables[0].Rows.Count;i++)
{
temp=int.Parse(myds.Tables[0].Rows[i].ItemArray[0].ToString());
try
{
if (myhash[temp]==null)
{myhash.Add(temp,true);}
}
catch(Exception ee)
{
string dd=ee.ToString();
}
} for(int i=from;i<=to;)
{ for(;i<=to;i++)
{
//label1.Text=string.Format(jiansuo,make7(i));
if (myhash[i]==null)
{
tempfrom=i;
break;
}
}
for(;i<=to;i++)
{
//label1.Text=string.Format(jiansuo,make7(i));
if (myhash[i]==null)
{
continue;
}
else
{
tempto=i-1;
i++;
break;
}
}
if (i<=to)
{
int m=tempto-tempfrom+1;
textBox1.Text=textBox1.Text+make7(tempfrom)+"-------"+make7(tempto)+"     "+m.ToString()+"个\r\n";
}
}
}
catch(Exception e)
{
string ttt=e.ToString();
}
this.textBox1.Text=textBox1.Text+"----检索完毕----";
this.btnSave.Enabled=true;
this.button1.Enabled=false;
return;
}

解决方案 »

  1.   

    在访问窗体控件的时候就抛出异常,最郁闷在于catch出来后就告诉我线程被终止。。
      

  2.   

    在窗体读入的时候加上 
    CheckForIllegalCrossThreadCalls =false;
    方法
    命名空间在
    System.windows.form
    做个好梦, 接分愉快
      

  3.   

    vs2005出于安全考虑,不容许这样的操作,即线程间相互调用,你在线程间更新界面的操作就触犯了这一点,解决的办法有以下两个:第一:在构造函数中加上这样一句话即可:
    CheckForIllegalCrossThreadCalls = false;
    加上这句话后,线程的处理就同vs2003完全一样了,即允许线程间相互调用第二:利用委托代理来实现:
    举个很简单的例子,比如我想在线程中对窗体中的textbox进行更新,可以这样来实现,如下:        //定义一个代理
            public delegate void MyInvoke(string str);
            //更新界面的方法
            private void UpdateTextBox(string str)
            {
                textBox1.Text +=str;
            }        //在线程回调函数里需要更新界面的地方,这样操作:
            .....
            MyInvoke mi = new MyInvoke(UpdateTextBox);
            this.BeginInvoke(mi, new object[] { "i" });
            .....
    楼主可以试下,也可以上网再查下资料...
      

  4.   

    this.label1.Text="正在分析数据...";
    textBox1.Text=textBox1.Text+make7(tempfrom)+"-------"+make7(tempto)+"     "+m.ToString()+"个\r\n";
    this.textBox1.Text=textBox1.Text+"----检索完毕----";
    this.btnSave.Enabled=true;
    this.button1.Enabled=false;
    去掉这几句看看
    多线程在使用控件成员的时候会出问题,用委托吧