private void RateWorker_DoWork(object sender, DoWorkEventArgs e) { try { DateTime sTime = (DateTime)e.Argument; StockAnalyser analyser = new StockAnalyser(); List <DateTime> dataList = new List <DateTime>(); RateWorker.ReportProgress(0, "Rate Start"); using (stockdbaEntities db = new stockdbaEntities()) { dataList.AddRange(db.DailySummary.Where(o => o.receiveDate >= sTime).GroupBy(o => o.receiveDate).Select(o => o.Key)); } foreach (var item in dataList) //Parallel.ForEach(dataList, item => { RateWorker.ReportProgress(0, item); analyser.DoDailyRate(item); analyser.DoWeeklyRate(item); analyser.DoTotalRate(item); }//); RateWorker.ReportProgress(0, "Rate Done"); } catch (Exception ex) { RateWorker.ReportProgress(0, ex.Message); } }
static void DetailQuery() { StockQuery worker = new StockQuery(); StockAnalyser analyser = new StockAnalyser(); DateTime receiveDate; List<DailyWork> workList = new List<DailyWork>(); ParallelOptions option = new ParallelOptions(); option.MaxDegreeOfParallelism = 10; int retryCnt = 0; Random rnd = new Random(); while (true) { using (stockdbaEntities db = new stockdbaEntities()) { receiveDate = db.DailyWork.OrderByDescending(o => o.receiveDate).First().receiveDate; workList.AddRange(db.DailyWork.Where(o => o.currentPage != o.totalPage && o.receiveDate == receiveDate)); } if (workList.Count() > 0) { Parallel.ForEach(workList, option, item => { try { Thread.Sleep(rnd.Next(100, 1000)); for (int i = (int)item.currentPage + 1; i <= (int)item.totalPage; i++) { worker.QuerySinglePage(i, item.stockId); using (stockdbaEntities db = new stockdbaEntities()) { db.DailyWork.Where(o => o.receiveDate == item.receiveDate && o.stockId == item.stockId).First().currentPage = i; db.ServiceLog.Add(new ServiceLog() { updateTime = DateTime.Now, updateLog = string.Format("Run:{0}:{1}/{2}", item.stockId, i, item.totalPage) }); db.SaveChanges(); Logger(string.Format("Run:{0}:{1}/{2}", item.stockId, i, item.totalPage)); if (i == item.totalPage) { string stockName = "-"; var obj = db.Company.Where(o => o.stockId == item.stockId).FirstOrDefault(); if (obj != null) { stockName = obj.name; } // 結算 analyser.DoSettlement(item.receiveDate, item.stockId); // 集中率 analyser.DoDailyRate(item.receiveDate, item.stockId, stockName); analyser.DoWeeklyRate(item.receiveDate, item.stockId, stockName); analyser.DoTotalRate(item.receiveDate, item.stockId, stockName); Logger(string.Format("Run:{0}: Settlement", item.stockId)); } } Thread.Sleep(10000); } } catch (Exception ex) { using (stockdbaEntities db = new stockdbaEntities()) { db.ServiceLog.Add(new ServiceLog() { updateTime = DateTime.Now, updateLog = String.Format("Run:{0}", ex.Message) }); db.SaveChanges(); } } }); workList.Clear(); } else { if (retryCnt < 60) { retryCnt++; Thread.Sleep(60000); // 1min } else { break; } } } }