0

我正在从 Yahoo 的“Stock API”更新我的单元格值。它的工作原理就像一个魅力,一切都在正确更新,但是当执行更新时,这几乎是所有时间,因为它们在一个间隔为 3000 毫秒的计时器中。我怎样才能摆脱这个问题?

我尝试暂停布局并恢复,但没有成功。

这是我自己的小自学项目,所以请记住,我以前从来没有用 xml 或 datagridview 做任何事情。

        private void timer2_Tick(object sender, EventArgs e)
    {

        try
        {
            if (!(dataGridView.Rows[0].Cells[0].Value == null)) // Updates the rows idividualy as long as first row is not empty
            {
                for (int i = 0; i < dataGridView.RowCount; i++)
                {
                    if (!(dataGridView.Rows[i].Cells[0].Value == null || dataGridView.Rows[i].Cells[0].Value.ToString() == "-")) // Makes sure that the row to update is not an empty row
                    {
                        String symbol;
                        symbol = Convert.ToString(dataGridView.Rows[i].Cells[0].Value);
                        String URLString2 = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22" + symbol + "%22)%0A%09%09&diagnostics=true&env=http%3A%2F%2Fdatatables.org%2Falltables.env";
                        dataGridView.ResumeLayout();
                        dataGridView.Update();
                        for (int t = 2; t < dataGridView.Columns.Count; t++) 
                        {
                            dataGridView.SuspendLayout();
                            XmlTextReader reader2 = new XmlTextReader(URLString2); // Makes the reader read from the string abow ( URL )
                            string NasdaqOpenTime = "09:00:00";

                            // if the market haven't been open for the day then theres no DaysLow value
                            if (dataGridView.Columns[t].HeaderText == "DaysLow" && DateTime.Now.CompareTo(DateTime.Parse(NasdaqOpenTime)) == -1)
                            {
                                dataGridView.Rows[i].Cells[dataGridView.Columns[t].Index].Value = "-"; 
                            }

                            // if the market haven't been open for the day then theres no DaysHigh value
                            if (dataGridView.Columns[t].HeaderText == "DaysHigh" && DateTime.Now.CompareTo(DateTime.Parse(NasdaqOpenTime)) == -1)
                            {
                                dataGridView.Rows[i].Cells[dataGridView.Columns[t].Index].Value = "-";
                            }
                            else
                            {
                                reader2.ReadToFollowing(dataGridView.Columns[t].HeaderText);  // Reada until it fins the elemnt Bid , then stops on it
                                reader2.ReadStartElement(dataGridView.Columns[t].HeaderText); // Recognizes Bid as start element (Bid)
                                dataGridView.Rows[i].Cells[dataGridView.Columns[t].Index].Value = reader2.ReadString(); // Reads the text in between (Declared as a string) actualy the bid value
                                reader2.ReadEndElement();  // Checks that the current nod is an end element (/Bid) if so then continue
                                reader2.ResetState();
                            }

                        }
                    }
                }
            }
        }
        catch (XmlException)
        {
        }
    }
4

1 回答 1

0

您最好在单独的线程中向 Yahoo 请求数据更新,因为这需要很长时间。您可以为此目的使用 System.Threading.Timer。

于 2014-02-19T20:39:15.613 回答