protected void Button1_Click(object sender, EventArgs e)
    {
        loading();
        ManualResetEvent[] mre = new ManualResetEvent[5];
        int Photocount = 0;
        try
        {
            SqlConnection sqlconn = new SqlConnection(ConfigurationManager.AppSettings[connectdate.getconnect()]);
            sqlconn.Open();            beginTime = this.TextBox1.Text;
            endTime = this.TextBox2.Text;
            photokind = this.DropDownList1.SelectedValue;
            if (beginTime == "" || endTime == "")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "a", "alert('请先选择时间段,系统将根据时间段进行自动识别。');", true);
            }
            else
            {
                string sqls = "";
                if (photokind == "0")
                {
                    sqls = "select count(*) as totals from photoxxx where climeTime between '" + beginTime + "' and '" + endTime + "'";
                }
                else
                {
                    sqls = "select count(*) as totals from photoxxx where climeTime between '" + beginTime + "' and '" + endTime + "' and photokind='" + photokind + "'";
                }                SqlDataAdapter myApters = new SqlDataAdapter(sqls, sqlconn);
                DataSet myDSs = new DataSet();
                myApters.Fill(myDSs);
                DataRowView rowViews = myDSs.Tables[0].DefaultView[0];
                Photocount = Convert.ToInt32(rowViews["totals"]);//34
                comNum = Convert.ToInt32(Math.Ceiling(Photocount*1.0 / 5));// 9
                //ThreadPool.SetMaxThreads(10,4);
               
                for (int i = 0; i < 5; i++)
                {
                    //Thread t = new Thread(new ParameterizedThreadStart(checkPhotos));
                    //t.Start(i);
                   
                    WaitCallback waitcallback = new WaitCallback(checkPhotos);
                    sumcount++;
                    ThreadPool.QueueUserWorkItem(waitcallback, i);
                    Thread.Sleep(1000);
                    mre[i] = new ManualResetEvent(false);
                }             
                           }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            while (!(sumnum >= Photocount))
            {
               
                 if(showWin(sumnum, Photocount))
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "a", "remove_loading();", true);
                    waitSucc = WaitHandle.WaitAll(mre, 1000, true);                 
                   break;
               }
            }
        }
    }
为什么感觉只走了一个线程啊? 我用两千条数据测试,只修改掉400条,3000条只修改600条。完全是五分之一的数据量。
为什么咧?

解决方案 »

  1.   

    代码不全,不过WaitAll的时间也太短了吧,1秒钟啊,你操作数据库能完成吗
      

  2.   


    其他的只是checkPhotos的方法。
    这个应该是线程的问题吧。
      

  3.   

    if (photokind == "0")
                    {
                        sqls = "select count(1) as totals from photoxxx where climeTime between '" + beginTime + "' and '" + endTime + "'";
                    }
                    else
                    {
                        sqls = "select count(1) as totals from photoxxx where climeTime between '" + beginTime + "' and '" + endTime + "' and photokind='" + photokind + "'";
                    }这样可以帮你节省点时间
      

  4.   

    说的有理.
    你用Stream将处理过程记录到log,看看.
      

  5.   


        public void checkPhotos(object TreadNo)
        {
            int treadnum = Int32.Parse(TreadNo.ToString());
            SqlConnection sqlconn2 = new SqlConnection(ConfigurationManager.AppSettings[connectdate.getconnect()]);
            sqlconn2.Open();
            try
            {
                string sqltop = "";
                if (treadnum == 0)
                {
                    if (photokind == "0")
                    {
                        sqltop = "select  top " + comNum + " photoid,photopath from photo where climeTime between '" + beginTime + "' and '" + endTime + "' ";
                    }
                    else
                    {
                        sqltop = "select top " + comNum + " photoid,photopath from photo where climeTime between '" + beginTime + "' and '" + endTime + "' and photokind='" + photokind + "' ";
                    }            }
                else
                {
                    if (photokind == "0")
                    {
                        sqltop = "select top " + comNum + " photoid,photopath from photo where climeTime between '" + beginTime + "' and '" + endTime + "' and photoid>(select max(photoid) from (select top " + treadnum * comNum + " photoid from photo where climeTime between '" + beginTime + "' and '" + endTime + "' order by photoid) a) '";
                    }
                    else
                    {
                        sqltop = "select top " + comNum + " photoid,photopath from photo where climeTime between '" + beginTime + "' and '" + endTime + "' and photoid>(select max(photoid) from (select top " + treadnum * comNum + " photoid from photo where climeTime between '" + beginTime + "' and '" + endTime + "'and photokind='" + photokind + "' order by photoid) a) and photokind='" + photokind + "'";
                    }            }            SqlDataAdapter adapter = new SqlDataAdapter(sqltop, sqlconn2);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                foreach (DataRow row in dt.Rows)
                {
                    sumnum++;
                    string path = row["photopath"].ToString();
                    int photoid = Convert.ToInt32(row["photoid"].ToString());
                    string filePath = Server.MapPath(path);//这里可以动态传递图片路径的
                    if (!File.Exists(filePath))
                    {
                        string sql = "update photoxxx set isUploadShibie='True' where photoid=" + photoid;
                        DataCom(sql);
                    }
                    else
                    {                    lock (SequenceLock)
                        {
                            //识别图片代码
                            EXIFMetaData em = new EXIFMetaData();                        EXIFMetaData.Metadata m = em.GetEXIFMetaData(filePath);//这里就是调用,传图片绝对路径                        string makesoftware = m.MakeSoftware.DisplayValue;//使用软件                        string datePictureTaken = m.DatePictureTaken.DisplayValue;//拍摄日期                        string sql1 = "";
                            //图片之前被Photoshop处理过
                            if (makesoftware != null)
                            {
                                if (makesoftware.IndexOf("Photoshop") != -1 || makesoftware.IndexOf("Adobe") != -1)
                                {
                                    sql1 = "update photoxxx set isps='True',isUploadShibie='True',makesoftware='Photoshop' where photoid=" + photoid;
                                    DataCom(sql1);
                                }
                                if (makesoftware.IndexOf("ACD Systems Digital Imaging") != -1)
                                {
                                    sql1 = "update photoxxx set isps='True',isUploadShibie='True',makesoftware='ACD Systems' where photoid=" + photoid;
                                    DataCom(sql1);
                                }
                            }                        if (datePictureTaken != null)
                            {
                                //图片是经过拍摄的
                                if (datePictureTaken.Length > 0)
                                {
                                    sql1 = "update photoxxx set isps='False',isUploadShibie='True' where photoid=" + photoid;
                                    DataCom(sql1);
                                }
                            }
                            else
                            {
                                //既不是拍摄的,又没有被photoshop处理过,则可能为扫描的或者网上下载的
                                FileStream fs = new FileStream(filePath, FileMode.Open);
                                StreamReader reader = new StreamReader(fs, Encoding.UTF8);                            string line = reader.ReadToEnd();//读取文件中的字符串
                                fs.Close();
                                while (line != null)
                                {
                                    //图片之前被PS过
                                    if (line.IndexOf("photoshop") != -1 || line.IndexOf("Adobe") != -1)
                                    {
                                        sql1 = "update photoxxx set isps='True',isUploadShibie='True',makesoftware='Photoshop' where photoid=" + photoid;
                                        DataCom(sql1);
                                        break;
                                    }
                                    if (line.IndexOf("ACD Systems Digital Imaging") != -1)
                                    {
                                        sql1 = "update photoxxx set isps='True',isUploadShibie='True',makesoftware='ACD Systems' where photoid=" + photoid;
                                        DataCom(sql1);
                                        break;
                                    }
                                    //图片之前没被PS过
                                    else
                                    {
                                        sql1 = "update photoxxx set isps='False',isUploadShibie='True' where photoid=" + photoid+" ";
                                        DataCom(sql1);
                                        break;
                                    }
                                }                        }
                        }                }            }
                sqlconn2.Close();
                //ClientScript.RegisterStartupScript(this.GetType(), "a", "alert('图片识别完成');", true);
            }
            catch (Exception ex)
            {
                //Thread.CurrentThread.Abort();
                throw ex;
            }
            finally
            {
                Thread.CurrentThread.Abort();        }    }
    这个是线程要执行的方法。心血,奉献出来了。