//Start serial connection public bool Start(List <SmpStock> lst_StockSelectedArray) { if (bgWorker.IsBusy) { return(false); } ApiState sas = new ApiState(); sas.Codes = new List <String>(); sas.Codes.Clear(); foreach (SmpStock stock in lst_StockSelectedArray) { if (stock.Checked) { sas.Codes.Add(stock.Code); } } if (sas.Codes.Count == 0) { sas.Mode = (Int16)ApiMode.MARKET; } else { sas.Mode = (Int16)ApiMode.STOCK; } //Pass through serial port to background worker Hashtable workerOptions = new Hashtable(); workerOptions.Add("mode", sas.Mode); workerOptions.Add("time", SLEEP_INTERVAL); workerOptions.Add("code", sas.Codes); workerOptions.Add("data", ""); //Init background worker bgWorker.RunWorkerAsync(workerOptions); if (debug) { Console.WriteLine("[INFO] 程序已经初始化"); } return(true); }
//Do work in background void bgWorker_DoWork(object sender, DoWorkEventArgs e) { ApiState sas = new ApiState(); double totalTime = 0; Int16 delay_time_ms = 100; List<StockType> stringResults = new List<StockType>(); //Clear any errors bgWorkedError = false; //Report process started bgWorker.ReportProgress(0, "程序已经启动"); if (debug) Console.WriteLine("[INFO] 程序已经启动"); //Read out passed in options Hashtable workeroptions; workeroptions = (Hashtable)e.Argument; bgWorker.ReportProgress(11, (short)workeroptions["mode"]); // "模式: " bgWorker.ReportProgress(12, (List<String>)workeroptions["code"]); // "参数: " bgWorker.ReportProgress(13, (string)workeroptions["data"]); // "数据:" delay_time_ms = (Int16)workeroptions["time"]; sas.Mode = (Int16)workeroptions["mode"]; sas.Codes = (List<String>)workeroptions["code"]; try { do { IndexType responeBuffer = null; IndexService indexService = new IndexService(); indexService.deserialize(); responeBuffer = indexService.IndexEntry; bgWorker.ReportProgress(50, (IndexType)responeBuffer); } while (false); foreach (String code in sas.Codes) { StockType responeBuffer = null; StockService stockService = new StockService(); if (debug) Console.WriteLine("[INFO] 数据请求"); DateTime dt_start = DateTime.Now; { if (!stockService.deserialize(code)) { Console.WriteLine("¥"); } responeBuffer = stockService.StockEntry; } DateTime dt_stop = DateTime.Now; stringResults.Add(responeBuffer); TimeSpan interval = dt_stop - dt_start; totalTime += interval.TotalMilliseconds; if (debug) Console.WriteLine("[INFO] 接收数据完成"); //If not awaiting cancellation if (!bgWorker.CancellationPending) { //Wait 100 ms before checking for more data Thread.Sleep(delay_time_ms); } else { break; } } Thread.Sleep(delay_time_ms); bgWorker.ReportProgress(99, totalTime); bgWorker.ReportProgress(100, stringResults); } catch (Exception ex) { //Report error message bgWorker.ReportProgress(0, ex.Message); if (debug) Console.WriteLine("[ERR] 程序异常"); //bgWorkedError = true; return; } }
//Start serial connection public bool Start(List<SmpStock> lst_StockSelectedArray) { if (bgWorker.IsBusy) { return false; } ApiState sas = new ApiState(); sas.Codes = new List<String>(); sas.Codes.Clear(); foreach (SmpStock stock in lst_StockSelectedArray) { if (stock.Checked) sas.Codes.Add(stock.Code); } if (sas.Codes.Count == 0) { sas.Mode = (Int16)ApiMode.MARKET; } else { sas.Mode = (Int16)ApiMode.STOCK; } //Pass through serial port to background worker Hashtable workerOptions = new Hashtable(); workerOptions.Add("mode", sas.Mode); workerOptions.Add("time", SLEEP_INTERVAL); workerOptions.Add("code", sas.Codes); workerOptions.Add("data", ""); //Init background worker bgWorker.RunWorkerAsync(workerOptions); if (debug) Console.WriteLine("[INFO] 程序已经初始化"); return true; }
//Do work in background void bgWorker_DoWork(object sender, DoWorkEventArgs e) { ApiState sas = new ApiState(); double totalTime = 0; Int16 delay_time_ms = 100; List <StockType> stringResults = new List <StockType>(); //Clear any errors bgWorkedError = false; //Report process started bgWorker.ReportProgress(0, "程序已经启动"); if (debug) { Console.WriteLine("[INFO] 程序已经启动"); } //Read out passed in options Hashtable workeroptions; workeroptions = (Hashtable)e.Argument; bgWorker.ReportProgress(11, (short)workeroptions["mode"]); // "模式: " bgWorker.ReportProgress(12, (List <String>)workeroptions["code"]); // "参数: " bgWorker.ReportProgress(13, (string)workeroptions["data"]); // "数据:" delay_time_ms = (Int16)workeroptions["time"]; sas.Mode = (Int16)workeroptions["mode"]; sas.Codes = (List <String>)workeroptions["code"]; try { do { IndexType responeBuffer = null; IndexService indexService = new IndexService(); indexService.deserialize(); responeBuffer = indexService.IndexEntry; bgWorker.ReportProgress(50, (IndexType)responeBuffer); } while (false); foreach (String code in sas.Codes) { StockType responeBuffer = null; StockService stockService = new StockService(); if (debug) { Console.WriteLine("[INFO] 数据请求"); } DateTime dt_start = DateTime.Now; { if (!stockService.deserialize(code)) { Console.WriteLine("¥"); } responeBuffer = stockService.StockEntry; } DateTime dt_stop = DateTime.Now; stringResults.Add(responeBuffer); TimeSpan interval = dt_stop - dt_start; totalTime += interval.TotalMilliseconds; if (debug) { Console.WriteLine("[INFO] 接收数据完成"); } //If not awaiting cancellation if (!bgWorker.CancellationPending) { //Wait 100 ms before checking for more data Thread.Sleep(delay_time_ms); } else { break; } } Thread.Sleep(delay_time_ms); bgWorker.ReportProgress(99, totalTime); bgWorker.ReportProgress(100, stringResults); } catch (Exception ex) { //Report error message bgWorker.ReportProgress(0, ex.Message); if (debug) { Console.WriteLine("[ERR] 程序异常"); } //bgWorkedError = true; return; } }