private void button3_Click(object sender, EventArgs e)
 {
     using (StockInfoEntities stockDb = new StockInfoEntities())
     {
         //同步昨日净值
         foreach (DataGridViewRow item in dataGridView1.Rows)
         {
             var stock     = (stock)item.DataBoundItem;
             var stockInfo = stockDb.stock.Find(stock.id);
             stockInfo.PrevEarnMoney = Convert.ToDecimal(item.Cells["earnMoney"].Value);
         }
         stockDb.SaveChanges();
     }
     MessageBox.Show("操作成功!");
 }
        private async Task BindGridViewAsync()
        {
            using (StockInfoEntities stockDb = new StockInfoEntities())
            {
                var Data = stockDb.stock.Where(t => t.type == "持有").ToList();
                MyStock = (from item in Data
                           select new stockExtend()
                {
                    id = item.id,
                    code = item.code,
                    buyPrice = item.buyPrice,
                    area = item.area,
                    count = item.count,
                    curPrice = 0,
                    name = item.name,
                    salePrice = item.salePrice,
                    type = item.type,
                    planEarnMoney = item.planEarnMoney,
                    PrevEarnMoney = item.PrevEarnMoney,
                    isShort = item.isShort
                }
                           ).ToList();
                stockBindingSource.DataSource = Data;


                foreach (var item in MyStock)
                {
                    var dict = await RequestHelper.GetStockMAInfoAsync(item.area.ToUpper() + item.code);

                    var tempStockInfo = stockDb.stock.Find(item.id);
                    tempStockInfo.MAInfos = "MA4:" + dict["MA4"].ToString() + ";";
                    item.DictMaInfos      = dict;
                    stockDb.SaveChanges();
                }
            }
        }
        private async Task GetCurStockPriceAsync()
        {
            //await Task.Delay(1000 * 10);

            List <Task <StockInfo> > tasks = new List <Task <StockInfo> >();

            using (StockInfoEntities stockDb = new StockInfoEntities())
            {
                foreach (stock item in stockDb.stock.Where(t => t.type == "持有").ToList())
                {
                    var tempTask = Task.Run <StockInfo>(async() =>
                    {
                        using (StockInfoEntities db = new StockInfoEntities())
                        {
                            var stockinfo = db.stockInfos.SingleOrDefault(t => t.code == item.code);
                            return(await RequestHelper.GetStockInfoAsync(stockinfo.area.ToUpper() + item.code));
                            //return new StockInfo();
                        }
                    });

                    tasks.Add(tempTask);
                }
            }

            var FinnalResult = await Task.WhenAll(tasks.ToArray());

            synContext.Post(x => toolStripStatusLabel1.Text = "上次操作时间:" + DateTime.Now.ToString(), null);

            foreach (DataGridViewRow item in dataGridView1.Rows)
            {
                var temp   = item.DataBoundItem as stock;
                var result = FinnalResult.FirstOrDefault(t => t.data.quote.code.ToLower() == temp.code);

                if (result != null)
                {
                    StaticInfo.StockStatus = result.data.market.status;
                    if (result.data.market.status == "休市" || result.data.market.status == "已收盘" || result.data.market.status == "休市")
                    {
                        synContext.Post(x => toolStripStatusLabel1.Text = "当前状态:" + result.data.market.status, null);
                        cts.Cancel();
                    }
                    decimal?earnMoney = (result.data.quote.current - temp.buyPrice) * temp.count;

                    var TempStock = MyStock.Find(t => t.code == temp.code);
                    MyStock.Find(t => t.code == temp.code).curPrice = result.data.quote.current;
                    //更新UI操作
                    synContext.Post(x =>
                    {
                        item.Cells["curPrice"].Value  = result.data.quote.current;
                        item.Cells["earnMoney"].Value = earnMoney;

                        item.Cells["TodayEarn"].Value = earnMoney - temp.PrevEarnMoney;

                        if (earnMoney > 0)
                        {
                            item.Cells["earnMoney"].Style.BackColor = Color.Red;
                        }

                        if (TempStock.DictMaInfos != null)
                        {
                            if (result.data.quote.current <= Convert.ToDecimal(TempStock.DictMaInfos["MA4"]))
                            {
                                string errMsg = "快点卖" + "[MA4:" + TempStock.DictMaInfos["MA4"] + "]";
                                if (TempStock.isShort.HasValue)
                                {
                                    if (TempStock.isShort.Value)
                                    {
                                        errMsg = errMsg + ";短线做成了长线,亏钱了吧!";
                                    }
                                    item.Cells["Tip"].Style.BackColor = Color.Red;
                                }
                                item.Cells["Tip"].Value = errMsg;
                            }
                            else
                            {
                                item.Cells["Tip"].Value = "继续拿" + "[MA4:" + TempStock.DictMaInfos["MA4"] + "]";
                            }
                        }

                        item.Cells["percent"].Value = result.data.quote.percent;
                    }, null);
                }
            }
        }