2014-08-27 67 views
-6

目標是從webservice收集股票信息,並將其存儲在列表中。 *創建第二個線程,每5秒調用一次按鈕點擊事件*繼續更新如何創建第二個線程,每5秒調用一次按鈕點擊

如何創建第二個線程每5秒調用一次按鈕點擊事件?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Xml; 
using System.Threading; 

namespace StockList 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     // class with stock values declared as properties 
     class StockClass 
     { 
      public string Symbol { get; set; } 
      public string Last { get; set; } 
      public string Date { get; set; } 
      public string Time { get; set; } 
      public string Change { get; set; } 
      public string Open { get; set; } 
      public string High { get; set; } 
      public string Low { get; set; } 
      public string Volume { get; set; } 
      public string MktCap { get; set; } 
      public string PreviousClose { get; set; } 
      public string PercentageChange { get; set; } 
      public string AnnRange { get; set; } 
      public string Earns { get; set; } 
      public string PE { get; set; } 
      public string Name { get; set; } 
     } 

     //storing each stock data in a StockList 
     List<StockClass> StockList = new System.Collections.Generic.List<StockClass>(); 

     //button will call GettingData method 
     public void button1_Click(object sender, EventArgs e) 
     { 
      GettingData(); 
     } 

     /*GettingData method gets data from webservice, converts string to xml and stored 
       in object myStock of type Stock */ 
     public void GettingData() 
     { 
      ServiceReference1.StockQuoteSoapClient client = new ServiceReference1.StockQuoteSoapClient("StockQuoteSoap"); 
      string result = client.GetQuote(textBox1.Text); 
      System.Data.DataTable dataTable = new DataTable(); 
      XmlDocument xmlDoc = new XmlDocument(); 
      xmlDoc.LoadXml(result); 
      XmlNodeList nodes = xmlDoc.SelectNodes("/StockQuotes/Stock"); 
      foreach (XmlNode xm in nodes) 
      { 
       var myStock = new StockClass() 
       { 
        Name = xm["Name"].InnerText, 
        Low = xm["Low"].Value, 
        Symbol = xm["Symbol"].InnerText, 
        Last = xm["Last"].InnerText, 
        Date = xm["Date"].InnerText, 
        Time = xm["Time"].InnerText, 
        Change = xm["Change"].InnerText, 
        Open = xm["Open"].InnerText, 
        High = xm["Open"].InnerText, 
        Volume = xm["Volume"].InnerText, 
        MktCap = xm["MktCap"].InnerText, 
        PreviousClose = xm["PreviousClose"].InnerText, 
        PercentageChange = xm["PercentageChange"].InnerText, 
        AnnRange = xm["AnnRange"].InnerText, 
        Earns = xm["Earns"].InnerText, 
        PE = xm["P-E"].InnerText, 
       }; 
       StockList.Add(myStock); 
      } 
      lblCompanyName.Text = StockList[StockList.Count - 1].Name; 
     } 
    } 
} 
+0

Web服務:http://www.webservicex.net/stockquote.asmx?WSDL – user3853658 2014-08-27 19:11:57

+3

爲什麼要叫'button1_click'?不能直接調用'GettingData'嗎? – Vland 2014-08-27 19:12:15

+0

雅甚至從第二個線程調用GettingData方法應該是好的 – user3853658 2014-08-27 19:15:59

回答

0
private Timer _timer = new Timer(state => button1_Click(this, new EventArgs(), null, 1, 5000); 

對於真正的工作線程使用例如Background WorkerTask

+0

謝謝,但我想知道以多線程方式嘗試它..我不知道它的多線程 – user3853658 2014-08-27 19:17:52

0

您可以輕鬆地使用Timer打電話,擁有5秒的時間間隔的方法,所有你所要做的就是創建一個定時器實例並且在Elapsed事件中調用需要每5秒調用一次的方法。

Place this code within Form's constructor

public Form1() 
{ 
    Timer timer = new Timer() { Enabled = true, Interval = 5000 }; 
    timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); 
    timer.Start(); 
} 

Also add the following event to your form

private void timer_Elapsed(object sender, ElapsedEventArgs e) 
{ 
    GettingData(); 
} 
+0

謝謝你會嘗試這個..你能告訴我我怎麼去用多線程的方式。繼續使用第二個線程更新 – user3853658 2014-08-27 19:19:54

相關問題