-6

Objective is to collect stock information from webservice , store it in list. * create second thread which will call button click event every 5 seconds * to keep updating

How do I create second thread to call button click event every 5 sec?

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;
        }
    }
}
gunr2171
  • 16,104
  • 25
  • 61
  • 88

2 Answers2

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

For real worker thread use for example Background Worker or Task

Community
  • 1
  • 1
Tomasito
  • 1,864
  • 1
  • 20
  • 43
0

You can easily use a Timer to call the method with 5 seconds interval, All you have to do is to create a timer instance with required timer interval and within the Elapsed event call the method that needs to be invoked for every 5 seconds.

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();
}
Kurubaran
  • 8,696
  • 5
  • 43
  • 65
  • Thanks will try this.. can you please tell me how can I go with this in multithread way. keep updating using second thread – user3853658 Aug 27 '14 at 19:19