public string Run() { Task nodeTask = new Task(() => { MainServiceReference.MainServiceSoapClient client = new MainServiceReference.MainServiceSoapClient(); MainServiceReference.WorkItem item; StockQuery worker = new StockQuery(); while ((item = client.GetWork()) != null) { try { using (stockdbaEntities db = new stockdbaEntities()) { db.ServiceLog.Add(new ServiceLog() { updateTime = DateTime.Now, updateLog = string.Format("Run:{0}", item.stockId) }); db.SaveChanges(); } for (int i = item.current + 1; i <= item.total; 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.total) }); db.SaveChanges(); } 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(); } } } Interlocked.Decrement(ref taskNum); }); if (taskNum < 10) { nodeTask.Start(); Interlocked.Increment(ref taskNum); return String.Format("New Task, TaskNum:{0}", taskNum); } return String.Format("TaskNum:{0}", taskNum); }
static void workInit() { StockQuery query = new StockQuery(); List<Task> taskList = new List<Task>(); taskList.Add(new Task(() => { query.QueryDaliyWork(); })); foreach (var item in taskList) { item.Start(); } Task.WaitAll(taskList.ToArray()); }
static void dailyInit() { StockQuery query = new StockQuery(); List<Task> taskList = new List<Task>(); taskList.Add(new Task(() => { query.QueryBroker(); })); taskList.Add(new Task(() => { query.QueryBrokerBranch(); })); taskList.Add(new Task(() => { query.QueryCompany(); })); foreach (var item in taskList) { item.Start(); } Task.WaitAll(taskList.ToArray()); taskList.Clear(); taskList.Add(new Task(() => { query.QueryDaliyWork(); })); foreach (var item in taskList) { item.Start(); } Task.WaitAll(taskList.ToArray()); }
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; } } } }
static void Main(string[] args) { MyEnum workType; StockQuery query = new StockQuery(); if (args.Length > 0) { Logger("---------------START---------------"); if (Enum.TryParse(args[0], true, out workType) == true) { switch (workType) { case MyEnum.Stage1: StockUtility util = new StockUtility(); Logger("Reset"); util.Reset(); //Logger("QueryBroker"); //query.QueryBroker(); //Logger("QueryBrokerBranch"); //query.QueryBrokerBranch(); //Logger("QueryCompany"); //query.QueryCompany(); Logger("QueryDaliyWork"); query.QueryDaliyWork(); Logger("QueryStockWarrant"); query.QueryStockWarrant(); break; case MyEnum.Stage1A: Logger("QueryDaliyWork"); query.QueryDaliyWork(); Logger("QueryStockWarrant"); query.QueryStockWarrant(); break; case MyEnum.Stage2: case MyEnum.Stage2A: DetailQuery(); break; case MyEnum.Stage3: case MyEnum.Stage3A: Logger("QueryOTC"); query.QueryOTC(); Logger("QueryOTCWarrant"); query.QueryOTCWarrant(DateTime.Now); break; default: break; } } Logger("----------------END----------------"); } }
private void TestWorker_DoWork(object sender, DoWorkEventArgs e) { try { TestWorker.ReportProgress(0, "Query Start"); StockQuery query = new StockQuery(); query.QueryOTC(); query.QueryOTCWarrant(DateTime.Now); TestWorker.ReportProgress(0, "Query End"); } catch (Exception ex) { TestWorker.ReportProgress(0, ex.Message); } }
private void SWWorker_DoWork(object sender, DoWorkEventArgs e) { try { SWWorker.ReportProgress(0, "Query Start"); StockQuery query = new StockQuery(); query.QueryStockWarrant(); SWWorker.ReportProgress(0, "Query End"); } catch (Exception ex) { SWWorker.ReportProgress(0, ex.Message); } }
private void MainWork_DoWork(object sender, DoWorkEventArgs e) { StockQuery query = new StockQuery(); List<Task> taskList = new List<Task>(); // 檢查是否為交易日 // Step 1. Query Basic Data taskList.Add(new Task(() => { query.QueryBroker(); })); taskList.Add(new Task(() => { query.QueryBrokerBranch(); })); taskList.Add(new Task(() => { query.QueryCompany(); })); foreach (var item in taskList) { item.Start(); } Task.WaitAll(taskList.ToArray()); taskList.Clear(); // Step 2. Query Stock Summary taskList.Add(new Task(() => { query.QueryDaliyWork(); })); foreach (var item in taskList) { item.Start(); } Task.WaitAll(taskList.ToArray()); // Check Progress // Daily Settlement // Daily Rate // Weekly Rate }
private void InitWorker_DoWork(object sender, DoWorkEventArgs e) { StockQuery query = new StockQuery(); switch ((int)e.Argument) { case 0: InitWorker.ReportProgress(0, "BasicData Start"); query.QueryBroker(); query.QueryBrokerBranch(); query.QueryCompany(); // query.QueryDaliyWork(); query.QueryStockWarrant(); InitWorker.ReportProgress(0, "BasicData Done"); break; case 1: InitWorker.ReportProgress(0, "StockData Start"); query.QueryDaliyWork(); query.QueryStockWarrant(); InitWorker.ReportProgress(0, "StockData Done"); break; default: break; } }