C# winform 多線程 實現實時對賬

1.winform 按鈕點擊事件

private void button1_Click(object sender, EventArgs e)
        {
            //啓動獲取商戶線程
            Thread tGetSH = new Thread(Globle.GetAllSH);
            tGetSH.Start();

            //啓動生成總帳單線程
            int count = Convert.ToInt32(this.numericUpDown1.Value);
            for (int i = 0; i < count; i++)
            {
                Thread tCreateBill = new Thread(new CreateBillMain().Run);
                tCreateBill.Start();
            }
        }

2.globle 多線程類
public class Globle
    {
       //public static List<Bill> SHList;
       public static List<Users> SHList;
       public static object objSH = new object();
       public static Random r = new Random();
       //public static BillBll bll = new BillBll();
       public static UsersBll bll = new UsersBll();
       static Globle() 
       {
           //SHList = new List<Bill>();
           SHList = new List<Users>();
       }

       public static void GetAllSH() 
       {
           DateTime date=DateTime.Now;
           while (true)
           {
               if (SHList.Count == 0)
               {
                   //List<Bill> list = bll.Search(x => x.BillMainID == null && x.State == 1).ToList();
                   List<Users> list = bll.Search(x => true).ToList();
                   lock (objSH)
                   {
                       foreach (var item in list)
                       {
                           SHList.Add(item);
                       }
                   }
               }
               Thread.Sleep(10*60*1000);
           }
       }

       public static Users GetSH() 
       {
           lock (objSH)
           {
               if (SHList.Count > 0)
               {
                   //Bill bl = SHList[r.Next(SHList.Count)];
                   Users bl = SHList[r.Next(SHList.Count)];
                   SHList.Remove(bl);
                   return bl;
               }
               return null;
           }
       }
    }
3.調用多線程,對錶進行操作

  public void Run()
        {
            while (true)
            {
                Users bl = Globle.GetSH();
                BillBll billbll = new BillBll();
                if (bl == null)
                {
                    Thread.Sleep(3000);
                    continue;
                }
                var usid = bl.ID;
                StoreBll store = new StoreBll();
                var stlist = store.Search(x => x.UserID == usid).Select(x => new { x.Strid, x.Strategy.RejectedDays }).ToList();
                if (stlist.Count == 0)
                {
                    Thread.Sleep(3000);
                    continue;
                }
                int billDay = Convert.ToInt32(stlist[0].RejectedDays);
                int day = DateTime.Now.Day;
                if (day <= billDay)
                {
                    continue;
                }
                DateTime end = DateTime.Now.AddDays(billDay - day + 1);
                DateTime start = end.AddMonths(-1);
                List<Bill> billlist = billbll.Search(x => x.Date <= end && x.Date >= start&&x.CusID==usid);
                var allMoney = billlist.Sum(x => x.Money);
                BillMain bm = new BillMain();
                BillMainBll bmbll = new BillMainBll();
                bm.Codes = "hh1234556778";
                bm.CusID = usid;
                bm.EndDate = end;
                bm.Money = allMoney;
                bm.StartDate = start;
                bm.State = 1;
                if (!bmbll.Add(bm))
                {
                    continue;
                };
                Message me = new Message();
                MessageBll mebll = new MessageBll();
                me.Contents = "爲" + bl.Name + "增加賬單總記錄";
                me.Date = DateTime.Now;
                me.State = 2;
                me.Stye = 1;
                me.Title = "賬單總記錄";
                me.CusID = bl.ID;
                mebll.AddNoSave(me);
                Bill bbl = new Bill();
                for (int i = 0; i < billlist.ToList().Count; i++)
                {
                    bbl = billlist.ToList()[i];
                    bbl.BillMainID = bm.ID;
                    billbll.ModifyNoSave(bbl);
                }
                if (bmbll.Save() > 0)
                {
                    Console.WriteLine("成功");
                    Console.Read();
                }
                
            }
        }


發佈了17 篇原創文章 · 獲贊 7 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章