var StartTime = DateTime.Now;            IList listProduct = bll.GetAllInfoByGIA();
            int succNum;
            var count = listProduct.Count / 5;
            var num1 = 0;
            System.Threading.Thread thread1 = new System.Threading.Thread(delegate()
              {
                  //执行抓取方法;
                  for (int i = 0; i < count; i++)
                  {
                      num1 += GatherGIA(listProduct[i]);
                  }              });
            thread1.Start();
            var num2 = 0;
            System.Threading.Thread thread2 = new System.Threading.Thread(delegate()
            {
                //执行抓取方法;
                for (int i = count; i < 2 * count; i++)
                {
                    num2 += GatherGIA(listProduct[i]);
                }
            });
            thread2.Start();
            var num3 = 0;
            System.Threading.Thread thread3 = new System.Threading.Thread(delegate()
            {
                //执行抓取方法;
                for (int i = 2 * count; i < 3 * count; i++)
                {
                    num3 += GatherGIA(listProduct[i]);
                }
            });
            thread3.Start();
            var num4 = 0;
            System.Threading.Thread thread4 = new System.Threading.Thread(delegate()
             {
                 //执行抓取方法;
                 for (int i = 3 * count; i < 4 * count; i++)
                 {
                     num4 += GatherGIA(listProduct[i]);
                 }
             });
            thread4.Start();
            var num5 = 0;
            System.Threading.Thread thread5 = new System.Threading.Thread(delegate()
             {
                 //执行抓取方法;
                 for (int i = 4 * count; i < listProduct.Count; i++)
                 {
                     num5 += GatherGIA(listProduct[i]);
                 }
             });
            thread5.Start();
            while (true)
            {
                if (thread1.ThreadState != System.Threading.ThreadState.Stopped) continue;
                if (thread2.ThreadState != System.Threading.ThreadState.Stopped) continue;
                if (thread3.ThreadState != System.Threading.ThreadState.Stopped) continue;
                if (thread4.ThreadState != System.Threading.ThreadState.Stopped) continue;
                if (thread5.ThreadState != System.Threading.ThreadState.Stopped) continue;
                break;
            }
            thread1.Abort();
            thread2.Abort();
            thread3.Abort();
            thread4.Abort();
            thread5.Abort();
            var EndTime = DateTime.Now;
            succNum = num1 + num2 + num3 + num4 + num5;
            string msg = string.Format("共有{0}颗GIA钻石,成功更新{1}颗GIA钻石参数,本次抓取总耗费{2}",
                listProduct.Count.ToString(), succNum.ToString(), EndTime - StartTime);
            this.lblGiaMsg.Text = msg;

解决方案 »

  1.   

    最好还是用线程池吧,Delegate.BeginInvoke会使用线程池,而且这样,你也不用写下边的那些等待逻辑(while true那些),直接EndInvoke来确定所有的异步方法执行完毕
      

  2.   

    CSDN果然没有以前那么火了 汗 
      

  3.   


    private delegate void GatherDelegate();
            private Object lockObj = new object();        public void Main()
            {
                DateTime StartTime = DateTime.Now;            IList listProduct = new List<String>();
                int succNum;
                Int32 count = listProduct.Count / 5;
                Int32 num1 = 0;            GatherDelegate delegate1 = delegate()
                                               {
                                                   //执行抓取方法;
                                                   for (int i = 0; i < count; i++)
                                                   {
                                                       lock (lockObj)
                                                       {
                                                           num1 += 1;
                                                       }
                                                   }                                           };            IAsyncResult result1 = delegate1.BeginInvoke(null, null);
                delegate1.EndInvoke(result1);
            }