Exemplo n.º 1
0
        static async void UserAsync()
        {
            using (MyTest1 db = new MyEFCodeFirstModel.MyTest1())
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();

                //List<Tuple<DateTime?, int, int>> timeListss = new List<Tuple<DateTime?, int, int>>();
                //timeListss.Add(null);
                //timeListss.Add(new Tuple<DateTime?, int, int>(DateTime.Now, 1, 1));
                //var count = timeListss.Count();

                DateTime?t1           = null;
                DateTime?t2           = null;
                DateTime?colltionTime = db.Collecction.Where(d => d.CateId == 38).FirstOrDefault() == null ? (new Collecction().CreateTime) : db.Collecction.Where(d => d.CateId == 38).FirstOrDefault().CreateTime;
                if (colltionTime == null)
                {
                    var dbTime = db.MyEntities.OrderBy(d => d.CreateTime).FirstOrDefault().CreateTime;
                    t1 = dbTime;
                    Collecction colletionmodel = new Collecction();
                    colletionmodel.CateId     = 38;
                    colletionmodel.CreateTime = dbTime;
                    db.Collecction.Add(colletionmodel);
                    db.SaveChanges();
                }
                else
                {
                    t1 = colltionTime;
                }
                t2 = t1.Value.AddDays(1);
                if (t1 <= DateTime.Now)
                {
                    List <Tuple <DateTime?, int, int> > timeList = new List <Tuple <DateTime?, int, int> >();

                    for (var i = 0; i < 6; i++)
                    {
                        timeList.Add(await TestListAsync(i, t1, t2));
                    }
                    sw.Stop();
                    TimeSpan ts2     = sw.Elapsed;
                    var      seconds = ts2.TotalSeconds;

                    var code = timeList.Where(d => d.Item1 != null).Sum(d => d.Item3);
                    if (code == 0)
                    {
                        Console.WriteLine("开始时间:{0}--结束时间:{1},更新:{2}数据 耗时:{3}", t1, t2, timeList.Sum(d => d.Item2), seconds);
                        var LastCreateTime = timeList.Max(d => d.Item1);
                        var upCollection   = db.Collecction.Where(d => d.CateId == 38).FirstOrDefault();
                        upCollection.CreateTime = LastCreateTime;
                        db.SaveChanges();
                    }
                    else
                    {
                        Console.WriteLine("开始时间:{0}--结束时间:{1},更新:{2}数据,采集出错事务回滚到时间:{3} 耗时:{4}", t1, t2, timeList.Sum(d => d.Item2), t1, seconds);
                    }
                }
            }
        }
Exemplo n.º 2
0
        static void TestDB()
        {
            while (true)
            {
                using (MyTest1 db = new MyEFCodeFirstModel.MyTest1())
                {
                    Stopwatch sw = new Stopwatch();
                    sw.Start();

                    //List<Tuple<DateTime?, int, int>> timeListss = new List<Tuple<DateTime?, int, int>>();
                    //timeListss.Add(null);
                    //timeListss.Add(new Tuple<DateTime?, int, int>(DateTime.Now, 1, 1));
                    //var count = timeListss.Count();

                    DateTime?t1           = null;
                    DateTime?t2           = null;
                    DateTime?colltionTime = db.Collecction.Where(d => d.CateId == 38).FirstOrDefault() == null ? (new Collecction().CreateTime) : db.Collecction.Where(d => d.CateId == 38).FirstOrDefault().CreateTime;
                    if (colltionTime == null)
                    {
                        var dbTime = db.MyEntities.OrderBy(d => d.CreateTime).FirstOrDefault().CreateTime;
                        t1 = dbTime;
                        Collecction colletionmodel = new Collecction();
                        colletionmodel.CateId     = 38;
                        colletionmodel.CreateTime = dbTime;
                        db.Collecction.Add(colletionmodel);
                        db.SaveChanges();
                    }
                    else
                    {
                        t1 = colltionTime;
                    }
                    t2 = t1.Value.AddDays(1);
                    if (t1 <= DateTime.Now)
                    {
                        List <Task> taskList    = new List <Task>();
                        TaskFactory taskFactory = new TaskFactory();
                        List <Tuple <DateTime?, int, int> > timeList = new List <Tuple <DateTime?, int, int> >();
                        for (int i = 1; i < 6; i++)
                        {
                            int ii = i;
                            taskList.Add(taskFactory.StartNew(() => timeList.Add(TestList(ii, t1, t2))));//开启一个线程   里面创建索引
                        }

                        Task.WaitAll(taskList.ToArray());

                        Console.WriteLine("开始结算>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

                        sw.Stop();
                        TimeSpan ts2     = sw.Elapsed;
                        var      seconds = ts2.TotalSeconds;

                        var code = timeList.Where(d => d.Item1 != null).Sum(d => d.Item3);
                        if (code == 0)
                        {
                            Console.WriteLine("开始时间:{0}--结束时间:{1},更新:{2}数据 耗时:{3}", t1, t2, timeList.Sum(d => d.Item2), seconds);
                            var LastCreateTime = timeList.Max(d => d.Item1);
                            var upCollection   = db.Collecction.Where(d => d.CateId == 38).FirstOrDefault();
                            upCollection.CreateTime = LastCreateTime;
                            db.SaveChanges();
                        }
                        else
                        {
                            Console.WriteLine("开始时间:{0}--结束时间:{1},更新:{2}数据,采集出错事务回滚到时间:{3} 耗时:{4}", t1, t2, timeList.Sum(d => d.Item2), t1, seconds);
                        }
                    }
                }
            }
        }