大数据量?应该也不算大啊 26318 条数据
代码://获取所有车次ID
public DataTable getAllCheci()
{
  String sql = "select Id from t_chec where datediff(second,getdate(),ftime)>0  and datediff(ss,ftime,getdate())<kaissp and datediff(ss,ftime,getdate())<KaiSYd";
     return AccessDal.SqlHelper.ExecuteDataset(Pub.Connectionstring, CommandType.Text, sql).Tables[0];
}
public void setValue(int value)
{
  progressBar2.Value = value;
  Thread.Sleep(1000);
}protected void Proce(object sender, EventArgs e)
{
   Application.DoEvents();
   DataTable dtCheci = getAllCheci();
   progressBar2.Maximum = dtCheci.Rows.Count;
   int j = 1;
   for (int i = 0; i < dtCheci.Rows.Count; i++)
   {
       string result = bcz.ChangeZuoXActionstringStatusByYuDTime(dtCheci.Rows[i]["Id"].ToString());
       Thread.Sleep(100);
       Invoke(new setProgressBarValue(setValue), j);
       j++;
       if (result != "")
       {
           String sql = "insert into Log(CZR,CZDate,CZIP,TableName,CZID,CZLX)values(@czr,@czdate,@czip,@table,@czid,@czlx)";
           SqlParameter[] param = new SqlParameter[] { 
                                             new SqlParameter("@czr",SqlDbType.Int,4),
                                             new SqlParameter("@czdate",SqlDbType.DateTime,8),
                                             new SqlParameter("@czip",SqlDbType.VarChar,50),
                                             new SqlParameter("@table",SqlDbType.VarChar,50),
                                             new SqlParameter("@czid",SqlDbType.Int,4),
                                             new SqlParameter("@czlx",SqlDbType.Int,4)
                                                       };
                    param[0].Value = Convert.ToInt32(Pub.Userid);
                    param[1].Value = DateTime.Now;
                    param[2].Value = Pub.LoginIp;
                    param[3].Value = "t_chec";
                    param[4].Value = Convert.ToInt32(dtCheci.Rows[i]["Id"].ToString());
                    param[5].Value = 1;
                    sp.ExecuteIntoLog(sql, param);
          }
                Thread.Sleep(100);
       }
    }private void testlx_Load(object sender, EventArgs e)
{
    this.BeginInvoke(new EventHandler(Proce), this, EventArgs.Empty);
}
为什么打开窗体 会卡死 请教高手 谢谢

解决方案 »

  1.   

    Application.DoEvents();加到循环里面,放到外面没什么用.
    另外为什么要加 Thread.Sleep(100); ?
      

  2.   

    姑且不说你的插入方法,至少你应该启动一个后台的工作线程来干这件事情。UI线程是用来显示UI的,不是用来操作数据的。具体可以参考Thread和BackgroundWorker两个类,MSDN上都有相应的实例代码。
      

  3.   

    不用多线程,居然还Thread.Sleep(100); 不卡死才怪.
      

  4.   

    先把 Thread.Sleep(100); 去掉,  如果觉得还是卡.就另开一个线程加载数据
      

  5.   

    还不知道怎么开 我先msdn上看看
      

  6.   

    Thread th=new Thread(fun);
    th.statrt();
      

  7.   

    Thread t = new Thread(new ThreadStart(你的方法名));
    t.start();注意ThreadStart这个委托的函数签名是public delegate void ThreadStart ()