时间:2021-07-01 10:21:17 帮助过:26人阅读
2.多线程执行过程:
//开始计时 Stopwatch sw = new Stopwatch(); sw.Start(); string sqlMember = ""; string sqlRoom = ""; List<Member> MemberList = null; List<Room> RoomList = null; using (RoomsEntities rooms = new RoomsEntities()) { var queryMember = rooms.Member.Where(u => u.IsStop == false); sqlMember = queryMember.ToString(); //这里ToString()后取到的是SQL语句 var queryRoom = rooms.Room.Take(10); sqlRoom = queryRoom.ToString(); } //开启两个线程执行 Task t1 = Task.Factory.StartNew(() => { MemberList = GetModelList<Member>(sqlMember); }); Task t2 = Task.Factory.StartNew(() => { RoomList = GetModelList<Room>(sqlRoom); }); Task.WaitAll(t1, t2); //等待两个线程完成,这里等待的t1,t2线程是同时执行的 sw.Stop(); TimeSpan ts2 = sw.Elapsed; Console.WriteLine("运行时间:" + ts2.TotalMilliseconds); Console.ReadKey();
多线程获取数据库数据
标签:nbsp logs summary base tor class stat 而且 top