public void UpdataSync(StockInfoEntity SIE) { Action<TextBox, String> updateAction = new Action<TextBox, string>(pricesync); price.Dispatcher.BeginInvoke(updateAction, price, SIE.price); Action<String> up = new Action<string>(updownsync); this.Dispatcher.BeginInvoke(up, SIE.updown); }
private void UpdataSync(StockInfoEntity SIE) { Action<TextBlock, String> updateAction = new Action<TextBlock, string>(TextBoxSync); arrow.Dispatcher.BeginInvoke(updateAction, arrow, SIE.arrow); high.Dispatcher.BeginInvoke(updateAction, high, SIE.high); low.Dispatcher.BeginInvoke(updateAction, low, SIE.low); open.Dispatcher.BeginInvoke(updateAction, open, SIE.open); percent.Dispatcher.BeginInvoke(updateAction, percent, Adapter.DataAdapter.RealTwo(Convert.ToDouble(SIE.percent) * 100) + "%"); price.Dispatcher.BeginInvoke(updateAction, price, SIE.price); time.Dispatcher.BeginInvoke(updateAction, time, SIE.time); turnover.Dispatcher.BeginInvoke(updateAction, turnover, Adapter.DataAdapter.RealTwo(Convert.ToDouble(SIE.turnover) / 100) + "手"); updown.Dispatcher.BeginInvoke(updateAction, updown, Adapter.DataAdapter.RealTwo(Convert.ToDouble(SIE.updown))); volume.Dispatcher.BeginInvoke(updateAction, volume, Adapter.DataAdapter.RealTwo(Convert.ToDouble(SIE.volume) / 10000) + "万"); yestclose.Dispatcher.BeginInvoke(updateAction, yestclose, SIE.yestclose); }
private void sync(StockInfoEntity SIE) { this.SIE = SIE;//委托修改 }
private string url = "http://api.money.126.net/data/feed/";//api地址 public override NET_ERROR StockGetWithCheck(ref List<string> id, out Dictionary<string, StockInfoEntity> dict) { dict = new Dictionary<string, StockInfoEntity>(); string stock = ""; foreach (string s in id) { stock += s + ','; } WebClient client = new WebClient(); client.Headers.Add("user-agent", "Mozilla/4.0"); Stream data; try { data = client.OpenRead(url + stock);//构建批量获取地址 } catch (WebException) { UpdateLog(stock, NET_ERROR.NET_CANT_CONNECT); return NET_ERROR.NET_CANT_CONNECT;//网络错误 无法连接 } StreamReader reader = new StreamReader(data);//读取所有结果 string str = reader.ReadToEnd(); string json; try { string pattern = @"\(.*?\)"; Regex regex = new Regex(pattern, RegexOptions.IgnoreCase); json = regex.Match(str).Value.Trim('(', ')');//正则获取json } catch (Exception) { UpdateLog(stock, NET_ERROR.NET_DATA_ERROR); return NET_ERROR.NET_DATA_ERROR;//获取json失败 } JObject jo; try { jo = (JObject)JsonConvert.DeserializeObject(json);//解析json } catch (JsonException) { UpdateLog(stock, NET_ERROR.NET_JSON_NOT_EXISTS); return NET_ERROR.NET_JSON_NOT_EXISTS;//解析失败 } StockInfoEntity SIE = new StockInfoEntity(); if (jo.Count == 0) { UpdateLog(stock, NET_ERROR.NET_REQ_ERROR); return NET_ERROR.NET_REQ_ERROR; } foreach (string s in id) { if (jo.Property(s) == null) { UpdateLog(s, NET_ERROR.NET_REQ_ERROR);//股票返回存在性检测 } else { try//尝试json读取股票数据 { JToken j = jo[s]; SIE.name = j["name"].ToString(); SIE.arrow = j["arrow"].ToString(); SIE.high = j["high"].ToString(); SIE.low = j["low"].ToString(); SIE.open = j["open"].ToString(); SIE.percent = j["percent"].ToString(); SIE.price = j["price"].ToString(); SIE.time = j["time"].ToString(); SIE.turnover = j["turnover"].ToString(); SIE.updown = j["updown"].ToString(); SIE.volume = j["volume"].ToString(); SIE.yestclose = j["yestclose"].ToString(); } catch (JsonException)//读取失败则放弃这股票 { UpdateLog(s, NET_ERROR.NET_REQ_ERROR);//股票读取失败 continue; } dict.Add(s, SIE);//成功获取可用数据放入dict中返回 } } return NET_ERROR.NET_REQ_OK; }