Example #1
0
        private void BackgroundWorker_RunWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorkerParam backgroundWorkerParam = (BackgroundWorkerParam)e.Argument;

            ShowMessage("子线程【" + backgroundWorkerParam.BatSerial + "】工作中...");
            try
            {
                SynData(backgroundWorkerParam);
            }
            catch (Exception ex)
            {
                ShowMessage("子线程【" + backgroundWorkerParam.BatSerial + "】异常:" + ex.Message);
            }
            ShowMessage("子线程【" + backgroundWorkerParam.BatSerial + "】工作完成");
        }
Example #2
0
 /// <summary>
 /// 数据处理操作
 /// </summary>
 /// <param name="backgroundWorkerParam"></param>
 private void SynData(BackgroundWorkerParam backgroundWorkerParam)
 {
     ShowMessage("开始处理第【" + backgroundWorkerParam.BatSerial + "】批数据...");
     try
     {
         if (null != backgroundWorkerParam.Orders && 0 < backgroundWorkerParam.Orders.Count)
         {
             for (int o = 0; o < backgroundWorkerParam.Orders.Count; o++)
             {
                 //ShowMessage(backgroundWorkerParam.Orders[o]);
                 Thread.Sleep(5);
             }
         }
         else
         {
             ShowMessage("第【" + backgroundWorkerParam.BatSerial + "】批数据个数为0");
         }
     }
     catch (Exception ex)
     {
         ShowMessage("处理第【" + backgroundWorkerParam.BatSerial + "】批数据异常" + ex.Message);
     }
     ShowMessage("第【" + backgroundWorkerParam.BatSerial + "】批数据处理完成.");
 }
Example #3
0
        private void threadMainWork()
        {
            IList <string> orderList = null;

            orderList = new List <string>();

            for (int i = 0; i < 19000; i++)
            {
                orderList.Add(i.ToString());
            }
            while (true)
            {
                if (null != orderList && orderList.Count > 0)
                {
                    int threads = orderList.Count % Seed == 0 ? orderList.Count / Seed : orderList.Count / Seed + 1;

                    ShowMessage(orderList.Count + "分" + threads + "批");
                    for (int t = 0; t < threads; t++)
                    {
                        manualResetEvent.WaitOne();//主线程暂停 直到所有任务完成
                        if (ThreadRun <= ThreadMax)
                        {
                            //ShowMessage("申请新线程:" + ThreadRun + " i【" + t + "】");
                            //ShowMessage("申请新线程:第【" + t + "】批");
                            BackgroundWorkerParam backgroundWorkerParam = new BackgroundWorkerParam();
                            backgroundWorkerParam.BatSerial  = t;
                            backgroundWorkerParam.StartIndex = Seed * t;
                            if (t == threads - 1)
                            {
                                backgroundWorkerParam.EndIndex = orderList.Count - 1;
                            }
                            else
                            {
                                backgroundWorkerParam.EndIndex = Seed * (t + 1) - 1;
                            }
                            backgroundWorkerParam.Orders = new List <string>();
                            for (int index = backgroundWorkerParam.StartIndex; index <= backgroundWorkerParam.EndIndex; index++)
                            {
                                backgroundWorkerParam.Orders.Add(orderList[index]);
                            }
                            ShowMessage("申请新线程:第【" + t + "】批列表序号【" + backgroundWorkerParam.StartIndex + "】-【" + backgroundWorkerParam.EndIndex + "】");
                            //新建进进程
                            BackgroundWorker backgroundWorker = new BackgroundWorker();
                            backgroundWorker.DoWork             += new DoWorkEventHandler(BackgroundWorker_RunWork);
                            backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackGroundWorker_RunWorkerCompleted);
                            backgroundWorker.RunWorkerAsync(backgroundWorkerParam);

                            Thread.Sleep(1000);
                            ThreadCountOprate("+");
                        }
                    }

                    //分配完之后主线程要设置成等待状态
                    ShowMessage("主线程开始等待所有子线程完成工作任务");
                    MainManualResetEvent.Reset();
                }
                else
                {
                    ShowMessage("没有数据");
                }

                MainManualResetEvent.WaitOne();//主线程暂停 直到所有任务完成
                ShowMessage("所有子线程完成工作任务 主线程继续");

                Thread.Sleep(5 * 60 * 1000);//5分钟
            }
        }