c# ThreadPool.QueueUserWorkItem线程池的应用

  • Post author:
  • Post category:其他




示例,将数据分批 ,每次1000条放入线程池中执行 dt 是查询出来的DataTable 数据。

    public List<AtmTaskLogdetail> OrderKPI_New(DateTime timeTo)
    {
			    List<AtmTaskLogdetail> tasklog = new List<AtmTaskLogdetail>();
                int calcmaxorder = 1000;
                int ordercounthas = dt.Rows.Count;
                int hasloop = ordercounthas / calcmaxorder;
                if (ordercounthas % calcmaxorder > 0)
                {
                    hasloop = hasloop + 1;
                }
                showLog("总订单数:" + ordercounthas, "构成表的数量:" + hasloop);

                Threadtasklog = new List<AtmTaskLogdetail>();
                for (int i = 1; i <= hasloop; i++)
                {
                    DataTable dtloop = dt.Clone();
                    int start = i * calcmaxorder - calcmaxorder;
                    int end = i * calcmaxorder;
                    for (int z = start; z < end; z++)
                    {
                        if (z >= ordercounthas) break;
                        dtloop.Rows.Add(dt.Rows[z].ItemArray);
                    }


                    ThreadPool.QueueUserWorkItem(new WaitCallback(Process), new Demo { dtloop = dtloop, linedata = linedata, username = username, stafflist = stafflist, tasklogList = tasklogList, clrloaddetail = clrloaddetail, countdetail = countdetail, clrccttotalpreloadList = clrccttotalpreloadList, clrtotalcctcountList = clrtotalcctcountList, pdatotalclrList = pdatotalclrList, inventoryinoutitemlist = inventoryinoutitemlist, listbox = listbox, receivebox = receivebox, task = task, clrtotalretailpreloadList = clrtotalretailpreloadList, ccttotalloaddetailList = ccttotalloaddetailList, retailloaddetailList = retailloaddetailList, logislist = logislist, linegooddt = linegooddt, clrtotalretailcountList = clrtotalretailcountList, pdaload = pdaload, tasktruckbind = tasktruckbind });


                 //   showLog("执行订单数量:", "总数:" + dtloop.Rows.Count);
                }
                #endregion

                while (!(ordercounthas == Threadtasklog.Count))
                {
                    Thread.Sleep(50);
                }
                tasklog.AddRange(Threadtasklog);

                return tasklog;
      }







 private class Demo
    {
        public DataTable dtloop { get; set; }
        public List<LineData> linedata { get; set; }
        public DataTable username { get; set; }
        public List<StaffWork> stafflist { get; set; }
        public List<tmplogs> tasklogList { get; set; }
        public DataTable clrloaddetail { get; set; }
        public DataTable countdetail { get; set; }
        public DataTable clrccttotalpreloadList { get; set; }
        public DataTable clrtotalcctcountList { get; set; }
        public DataTable pdatotalclrList { get; set; }
        public DataTable inventoryinoutitemlist { get; set; }
        public DataTable listbox { get; set; }
        public DataTable receivebox { get; set; }
        public DataTable task { get; set; }
        public DataTable clrtotalretailpreloadList { get; set; }
        public DataTable ccttotalloaddetailList { get; set; }
        public DataTable retailloaddetailList { get; set; }
        public DataTable logislist { get; set; }
        public DataTable tasktruckbind { get; set; }
        public DataTable linegooddt { get; set; }
        public DataTable clrtotalretailcountList { get; set; }
        public List<tmppdaload> pdaload { get; set; }

    }





    static List<AtmTaskLogdetail> Threadtasklog = new List<AtmTaskLogdetail>();
    private static void Process(object DemoList)
    {
            Demo List = DemoList as Demo;
            DataTable dtloop = List.dtloop;
            List<LineData> linedata = List.linedata;
            DataTable username = List.username;
            List<StaffWork> stafflist = List.stafflist;
            List<tmplogs> tasklogList = List.tasklogList;
            DataTable clrloaddetail = List.clrloaddetail;
            DataTable countdetail = List.countdetail;
            DataTable clrccttotalpreloadList = List.clrccttotalpreloadList;
            DataTable clrtotalcctcountList = List.clrtotalcctcountList;
            DataTable pdatotalclrList = List.pdatotalclrList;
            DataTable inventoryinoutitemlist = List.inventoryinoutitemlist;
            DataTable listbox = List.listbox;
            DataTable receivebox = List.receivebox;
            DataTable task = List.task;
            DataTable clrtotalretailpreloadList = List.clrtotalretailpreloadList;
            DataTable ccttotalloaddetailList = List.ccttotalloaddetailList;
            DataTable retailloaddetailList = List.retailloaddetailList;
            DataTable logislist = List.logislist;
            DataTable tasktruckbind = List.tasktruckbind;
            DataTable linegooddt = List.linegooddt;
            DataTable clrtotalretailcountList = List.clrtotalretailcountList;
            List<tmppdaload> pdaload = List.pdaload;



                AtmTaskLogdetail log = new AtmTaskLogdetail();
                log.TaskDetailId = item["orderid"].ToStr();


			    lock (Threadtasklog)
                {
                    Threadtasklog.Add(log);
                }
   	}                



版权声明:本文为xiongliuyun原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。