示例#1
0
 //消息提醒
 private void OutMsg()
 {
     ThreadPool.QueueUserWorkItem(o =>
     {
         while (true)
         {
             try
             {
                 if (queueMsgList.Count > 0)
                 {
                     //从队列中拿出数据
                     ErrorMsg eMsg = queueMsgList.Dequeue();
                     this.notifyIconMsg.ShowBalloonTip(eMsg.ShowTime, GetDescription(typeof(ErrorTitleType),Convert.ToInt32(eMsg.TitleType)), eMsg.Content, eMsg.ToolTripType);
                     Thread.Sleep(eMsg.ShowTime);
                 }
                 else
                 {
                     Thread.Sleep(30);//避免了CPU空转。
                 }
             }
             catch (Exception ex)
             {
                 ErrorMsg eMsg = new ErrorMsg();
                 eMsg.TitleType = (int)ErrorTitleType.CWMsg;
                 eMsg.Content = ex.Message;
                 eMsg.CreateTime = DateTime.Now;
                 eMsg.ShowTime = 4000;
                 eMsg.ToolTripType = ToolTipIcon.Error;
                 queueMsgList.Enqueue(eMsg);
                 log.Error(string.Format("读取队列数据异常,异常信息:{0} ;异常详情:{1}", ex.Message, ex));
             }
         }
     });
 }
示例#2
0
        public void Show(int fCount)
        {
            List<APIStock> listStock = new List<APIStock>();
            List<APIMarket> listMarket = new List<APIMarket>();
            try
            {
                //if(!((DateTime.Now.Hour > 9 && DateTime.Now.Hour < 12) || (DateTime.Now.Hour >= 13 && DateTime.Now.Hour < 16)))
                //{
                //    return;
                //}
                string sUrl = Convert.ToString(ConfigurationManager.AppSettings["GPURL"]);
                //stockid=sz002673,sh600030,sz002673,sz002673,sz002673,sz002673,sz002673,sz002673,sz002673,sz002673&list=1
                string stockidList = ConfigurationManager.AppSettings["GPPamamList"].Trim();
                if (string.IsNullOrEmpty(stockidList))
                {
                    MessageBox.Show("请先添加股票");
                    return;
                }
                string sParam = "stockid=" + stockidList + "&list=1";
                string sMessage = HttpGet(sUrl, sParam);
                if (!string.IsNullOrEmpty(sMessage))
                {
                    JObject jo = (JObject)JsonConvert.DeserializeObject(sMessage);
                    string sReturnCode = Convert.ToString(jo["errNum"]);
                    string sReturnMsg = Convert.ToString(jo["errMsg"]);
                    if (string.IsNullOrEmpty(sReturnCode))
                    {
                        log.Error("请求异常,次数:" + fCount);
                        //MessageBox.Show("请求异常");
                        this.Text = "请求异常";
                        return;
                    }
                    int mReturnCode = Convert.ToInt32(sReturnCode);
                    if (mReturnCode == (int)ErrorCode.Success)
                    {
                        string sReturnContent = Convert.ToString(jo["retData"]);
                        if (!string.IsNullOrEmpty(sReturnContent))
                        {
                            JObject jContent = (JObject)JsonConvert.DeserializeObject(sReturnContent);
                            string sGSList = Convert.ToString(jContent["stockinfo"]);//上市公司列表
                            string sDPList = Convert.ToString(jContent["market"]);//上证,深证
                            int mGSCount = jContent["stockinfo"].AsQueryable().Count();
                            //涨跌判断
                            List<APIStock> listGPYJ = GetGPYJList();
                            for (int i = 0; i < mGSCount; i++)
                            {
                                APIStock stock = new APIStock();
                                JObject jStock = (JObject)jContent["stockinfo"][i];
                                stock.name = Convert.ToString(jStock["name"]).Trim('"');
                                if (string.IsNullOrEmpty(stock.name))
                                {
                                    continue;
                                }
                                stock.SysNo = Convert.ToString(jStock["code"]).Trim('"');
                                stock.currentPrice = Convert.ToString(jStock["currentPrice"]);
                                string mIncrease = Convert.ToString(Math.Round(Convert.ToDouble(jStock["increase"].ToString()), 2, MidpointRounding.AwayFromZero));//涨跌幅
                                if (BIsTX == true)
                                {
                                    int txT = 1;
                                    if (string.IsNullOrEmpty(BTXTime) || int.TryParse(BTXTime, out txT) == false)
                                    {
                                        txT = 1;
                                    }
                                    else
                                    {
                                        txT = Convert.ToInt32(BTXTime);
                                    }
                                    //当前股票已提醒的间隔时间
                                    var mGPYTX = (from t in queueTXMsgList where t.Code == stock.SysNo && t.CreateTime >= DateTime.Now.AddMinutes(-txT) select t).ToList();
                                    if (mGPYTX.Count <= 0)
                                    {
                                        if (listGPYJ != null && listGPYJ.Count > 0)
                                        {
                                            var mGP = (from t in listGPYJ where t.SysNo == stock.SysNo && t.ISYJ == 1 select t).ToList();
                                            if (mGP.Count > 0)
                                            {
                                                string returnMsg = PriceYJ(stock.currentPrice, mIncrease, mGP[0]);
                                                if (!string.IsNullOrEmpty(returnMsg))
                                                {
                                                    ErrorMsg eMsg = new ErrorMsg();
                                                    eMsg.Code = stock.SysNo;
                                                    eMsg.TitleType = (int)ErrorTitleType.GJMsg;
                                                    eMsg.Content = returnMsg;
                                                    eMsg.CreateTime = DateTime.Now;
                                                    eMsg.ShowTime = 5000;
                                                    eMsg.ToolTripType = ToolTipIcon.Warning;
                                                    queueMsgList.Enqueue(eMsg);
                                                    queueTXMsgList.Enqueue(eMsg);
                                                    //this.notifyIconMsg.ShowBalloonTip(5000, "消息提醒", returnMsg, ToolTipIcon.Warning);
                                                }
                                            }
                                        }
                                    }
                                }
                                
                                stock.name = Convert.ToString(jStock["name"]).Trim('"');
                                stock.code = Convert.ToString(jStock["code"]).Trim('"').Substring(2);
                                stock.date = jStock["time"].ToString().Trim('"');
                                stock.openningPrice = Convert.ToString(jStock["OpenningPrice"]);
                                stock.closingPrice = Convert.ToString(jStock["closingPrice"]);
                                stock.hPrice = Convert.ToString(jStock["hPrice"]);
                                stock.lPrice = Convert.ToString(jStock["lPrice"]);

                                //stock.growth = Convert.ToString(jStock["growth"]);
                                //stock.growthPercent = Convert.ToString(jStock["growthPercent"]);
                                //stock.dealnumber = Convert.ToString(jStock["dealnumber"]);
                                stock.turnover = Convert.ToString(Math.Round(Convert.ToDouble(jStock["turnover"].ToString()) / 100000000, 2, MidpointRounding.AwayFromZero));
                                //stock.hPrice52 = Convert.ToString(jStock["52hPrice"]);
                                //stock.lPrice52 = Convert.ToString(jStock["52lPrice"]);

                                stock.competitivePrice = Convert.ToString(jStock["competitivePrice"]);
                                stock.auctionPrice = Convert.ToString(jStock["auctionPrice"]);
                                stock.totalNumber = Convert.ToString(Math.Round(Convert.ToDouble(jStock["totalNumber"].ToString()) / 1000000, 2, MidpointRounding.AwayFromZero));
                                stock.increase = (mIncrease.Substring(0, 1) == "-") ? mIncrease.Substring(1) : mIncrease;
                                listStock.Add(stock);
                            }
                            JObject jMarket = (JObject)jContent["market"];
                            APIMarket mSH = new APIMarket();
                            mSH.name = jMarket["shanghai"]["name"].ToString().Trim('"');
                            mSH.curdot = Convert.ToString(Math.Round(Convert.ToDouble(jMarket["shanghai"]["curdot"].ToString()), 2, MidpointRounding.AwayFromZero));
                            mSH.curprice = Convert.ToString(Math.Round(Convert.ToDouble(jMarket["shanghai"]["curprice"].ToString()), 2, MidpointRounding.AwayFromZero));
                            mSH.rate = Convert.ToString(Math.Round(Convert.ToDouble(jMarket["shanghai"]["rate"].ToString()), 2, MidpointRounding.AwayFromZero));
                            mSH.dealnumber = Convert.ToString(Math.Round(Convert.ToDouble(jMarket["shanghai"]["dealnumber"].ToString()) / 100000000, 2, MidpointRounding.AwayFromZero));
                            mSH.turnover = Convert.ToString(Math.Round(Convert.ToDouble(jMarket["shanghai"]["turnover"].ToString()) / 10000, 2, MidpointRounding.AwayFromZero));
                            APIMarket mSZ = new APIMarket();
                            mSZ.name = Convert.ToString(jMarket["shenzhen"]["name"]).Trim('"');
                            mSZ.curdot = Convert.ToString(Math.Round(Convert.ToDouble(jMarket["shenzhen"]["curdot"].ToString()), 2, MidpointRounding.AwayFromZero));
                            mSZ.curprice = Convert.ToString(Math.Round(Convert.ToDouble(jMarket["shenzhen"]["curprice"].ToString()), 2, MidpointRounding.AwayFromZero));
                            mSZ.rate = Convert.ToString(Math.Round(Convert.ToDouble(jMarket["shenzhen"]["rate"].ToString()), 2, MidpointRounding.AwayFromZero));
                            mSZ.dealnumber = Convert.ToString(Math.Round(Convert.ToDouble(jMarket["shenzhen"]["dealnumber"].ToString()) / 100000000, 2, MidpointRounding.AwayFromZero));
                            mSZ.turnover = Convert.ToString(Math.Round(Convert.ToDouble(jMarket["shenzhen"]["turnover"].ToString()) / 10000, 2, MidpointRounding.AwayFromZero));
                            listMarket.Add(mSH);
                            listMarket.Add(mSZ);
                        }
                    }
                    else if (!Enum.IsDefined(typeof(ErrorCode), mReturnCode))
                    {
                        log.Error("返回未知异常,次数:" + fCount + ",异常编号:" + mReturnCode + ";异常说明:" + sReturnMsg);
                        //MessageBox.Show("返回未知异常,异常编号:" + mReturnCode + ";异常说明:" + sReturnMsg);
                        this.Text = "返回未知异常,异常编号:" + mReturnCode + ";异常说明:" + sReturnMsg;
                        return;
                    }
                    else
                    {
                        MessageBox.Show(GetDescription(typeof(ErrorCode), mReturnCode));
                    }
                }
                else
                {
                    log.Error("返回数据空,次数:" + fCount);
                    //MessageBox.Show("返回数据空");
                    this.Text = "返回数据空";
                    return;
                }
            }
            catch (Exception ex)
            {
                log.Error("获取股票数据1,出现异常:"+ex.Message+";异常详情:" + ex);
            }

            try
            {
                if (listMarket.Count > 0)
                {
                    APIMarket apiSHZS = listMarket[0];
                    double shRate = Convert.ToDouble(apiSHZS.rate);
                    Color shColor = Color.Red;
                    string shZF = "+";
                    string shJT = "↑";
                    if (shRate >= 0)
                    {
                        shColor = Color.Red;
                        shZF = "+";
                        shJT = "↑";
                    }
                    else
                    {
                        shColor = Color.Green;
                        shZF = "";
                        shJT = "↓";
                    }
                    lblSHZS1.Text = apiSHZS.name;
                    lblSHZS1.ForeColor = shColor;
                    lblSHZS2.Text = shJT + apiSHZS.curdot + " " + shZF + apiSHZS.rate + "%";
                    lblSHZS2.ForeColor = shColor;
                    //lblSHZS3.Text = shZF + apiSHZS.curprice + " " + shZF + apiSHZS.rate + "%";
                    //lblSHZS3.ForeColor = shColor;
                    APIMarket apiSZZS = listMarket[1];
                    double szRate = Convert.ToDouble(apiSZZS.rate);
                    Color szColor = Color.Red;
                    string szZF = "+";
                    string szJT = "↑";
                    if (szRate >= 0)
                    {
                        szColor = Color.Red;
                        szZF = "+";
                        szJT = "↑";
                    }
                    else
                    {
                        szColor = Color.Green;
                        szZF = "";
                        szJT = "↓";
                    }
                    lblSZZS1.Text = apiSZZS.name;
                    lblSZZS1.ForeColor = szColor;
                    lblSZZS2.Text = szJT + apiSZZS.curdot + " " + szZF + apiSZZS.rate + "%";
                    lblSZZS2.ForeColor = szColor;
                    //lblSZZS3.Text = szZF + apiSZZS.curprice + " " + szZF + apiSZZS.rate + "%";
                    //lblSZZS3.ForeColor = szColor;
                }
            }
            catch (Exception ex)
            {
                log.Error("获取股票数据2,出现异常:"+ex.Message+";异常详情:" + ex);
            }

            try
            {
                lock (locker)
                {
                    #region 绑定数据到DataGridView

                    dataGridView1.Columns.Clear();
                    if (listStock.Count > 0)
                    {
                        //dataGridView1.Columns[4].DataPropertyName
                        DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn();
                        col1.HeaderText = "股票名称";
                        col1.DataPropertyName = "name";
                        col1.Name = "name";
                        col1.Width = 80;
                        col1.ReadOnly = true;
                        col1.Frozen = true;
                        //col1.ContextMenuStrip = contextMenuStripGridView;
                        dataGridView1.Columns.Insert(0, col1);
                        DataGridViewTextBoxColumn col3 = new DataGridViewTextBoxColumn();
                        col3.HeaderText = "当前价";
                        col3.DataPropertyName = "currentPrice";
                        col3.Name = "currentPrice";
                        col3.Width = 70;
                        col3.ReadOnly = true;
                        //col3.ContextMenuStrip = contextMenuStripGridView;
                        dataGridView1.Columns.Insert(1, col3);
                        DataGridViewTextBoxColumn col18 = new DataGridViewTextBoxColumn();
                        col18.HeaderText = "涨幅(%)";
                        col18.DataPropertyName = "increase";
                        col18.Name = "increase";
                        col18.Width = 75;
                        col18.ReadOnly = true;
                        //col18.ContextMenuStrip = contextMenuStripGridView;
                        dataGridView1.Columns.Insert(2, col18);
                        DataGridViewTextBoxColumn col2 = new DataGridViewTextBoxColumn();
                        col2.HeaderText = "股票代码";
                        col2.DataPropertyName = "code";
                        col2.Name = "code";
                        col2.Width = 80;
                        col2.ReadOnly = true;
                        //col2.ContextMenuStrip = contextMenuStripGridView;
                        dataGridView1.Columns.Insert(3, col2);
                        DataGridViewTextBoxColumn col6 = new DataGridViewTextBoxColumn();
                        col6.HeaderText = "今日最高价";
                        col6.DataPropertyName = "hPrice";
                        col6.Name = "hPrice";
                        col6.Width = 90;
                        col6.ReadOnly = true;
                        dataGridView1.Columns.Insert(4, col6);
                        DataGridViewTextBoxColumn col7 = new DataGridViewTextBoxColumn();
                        col7.HeaderText = "今日最低价";
                        col7.DataPropertyName = "lPrice";
                        col7.Name = "lPrice";
                        col7.Width = 90;
                        col7.ReadOnly = true;
                        dataGridView1.Columns.Insert(5, col7);
                        DataGridViewTextBoxColumn col8 = new DataGridViewTextBoxColumn();
                        col8.HeaderText = "开盘价";
                        col8.DataPropertyName = "openningPrice";
                        col8.Name = "openningPrice";
                        col8.Width = 70;
                        col8.ReadOnly = true;
                        dataGridView1.Columns.Insert(6, col8);
                        DataGridViewTextBoxColumn col9 = new DataGridViewTextBoxColumn();
                        col9.HeaderText = "昨日收盘价";
                        col9.DataPropertyName = "closingPrice";
                        col9.Name = "closingPrice";
                        col9.Width = 90;
                        col9.ReadOnly = true;
                        dataGridView1.Columns.Insert(7, col9);
                        DataGridViewTextBoxColumn col17 = new DataGridViewTextBoxColumn();
                        col17.HeaderText = "成交量(万)";
                        col17.DataPropertyName = "totalNumber";
                        col17.Name = "totalNumber";
                        col17.Width = 90;
                        col17.ReadOnly = true;
                        dataGridView1.Columns.Insert(8, col17);
                        DataGridViewTextBoxColumn col13 = new DataGridViewTextBoxColumn();
                        col13.HeaderText = "成交金额(亿)";
                        col13.DataPropertyName = "turnover";
                        col13.Name = "turnover";
                        col13.Width = 100;
                        col13.ReadOnly = true;
                        dataGridView1.Columns.Insert(9, col13);
                        DataGridViewTextBoxColumn col14 = new DataGridViewTextBoxColumn();
                        col14.HeaderText = "刷新日期";
                        col14.DataPropertyName = "date";
                        col14.Name = "date";
                        col14.Width = 90;
                        col14.ReadOnly = true;
                        dataGridView1.Columns.Insert(10, col14);

                        //DataGridViewTextBoxColumn col4 = new DataGridViewTextBoxColumn();
                        //col4.HeaderText = "涨幅比例";
                        //col4.DataPropertyName = "growthPercent";
                        //col4.Name = "growthPercent";
                        //col4.Width = 100;
                        //col4.ReadOnly = true;
                        //dataGridView1.Columns.Add(col4);
                        //DataGridViewTextBoxColumn col5 = new DataGridViewTextBoxColumn();
                        //col5.HeaderText = "价格涨幅";
                        //col5.DataPropertyName = "growth";
                        //col5.Name = "growth";
                        //col5.Width = 100;
                        //col5.ReadOnly = true;
                        //dataGridView1.Columns.Add(col5);


                        //DataGridViewTextBoxColumn col10 = new DataGridViewTextBoxColumn();
                        //col10.HeaderText = "52周最高价";
                        //col10.DataPropertyName = "hPrice52";
                        //col10.Name = "hPrice52";
                        //col10.Width = 100;
                        //col10.ReadOnly = true;
                        //dataGridView1.Columns.Add(col10);
                        //DataGridViewTextBoxColumn col11 = new DataGridViewTextBoxColumn();
                        //col11.HeaderText = "52周最低价";
                        //col11.DataPropertyName = "lPrice52";
                        //col11.Name = "lPrice52";
                        //col11.Width = 100;
                        //col11.ReadOnly = true;
                        //dataGridView1.Columns.Add(col11);
                        //DataGridViewTextBoxColumn col12 = new DataGridViewTextBoxColumn();
                        //col12.HeaderText = "成交量股";
                        //col12.DataPropertyName = "dealnumber";
                        //col12.Name = "dealnumber";
                        //col12.Width = 100;
                        //col12.ReadOnly = true;
                        //dataGridView1.Columns.Add(col12);

                        //DataGridViewTextBoxColumn col15 = new DataGridViewTextBoxColumn();
                        //col15.HeaderText = "未知competitivePrice";
                        //col15.DataPropertyName = "competitivePrice";
                        //col15.Name = "competitivePrice";
                        //col15.Width = 100;
                        //col15.ReadOnly = true;
                        //dataGridView1.Columns.Add(col15);
                        //DataGridViewTextBoxColumn col16 = new DataGridViewTextBoxColumn();
                        //col16.HeaderText = "未知auctionPrice";
                        //col16.DataPropertyName = "auctionPrice";
                        //col16.Name = "auctionPrice";
                        //col16.Width = 100;
                        //col16.ReadOnly = true;
                        //dataGridView1.Columns.Add(col16);

                        //dataGridView1.AllowUserToAddRows = false;
                        //dataGridView1.AllowUserToOrderColumns = true;        //允许用户调整列的位置
                        //dataGridView1.Columns[0].Visible = false; 
                        //dataGridView1.ColumnHeadersVisible = false; // 列头隐藏 
                        //dataGridView1.RowHeadersVisible = false; // 行头隐藏
                        dataGridView1.ReadOnly = true;
                        dataGridView1.AutoGenerateColumns = false;

                        dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                        dataGridView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                        dataGridView1.DataSource = listStock;
                        //http://wsh1798.iteye.com/blog/601592
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                log.Error("获取股票数据3,出现异常:"+ex.Message+";异常详情:" + ex);
            }

            try
            {
                //ShowHGT();
            }
            catch (Exception ex)
            {
                log.Error("获取股票数据4,出现异常:"+ex.Message+";异常详情:" + ex);
            }
        }