0

I implemented a function in a windows form application to capture and read needed tabular data from a file (sourcedata.data) and save it in another file (result.data ). How i and by using the application can capture a real time stream data like such available here :https://data.sparkfun.com/streams in csv or .data file to use it. Or are there any direct waya to read the stream data directly from the website source periodically ?

private void button5_Click(object sender, EventArgs e)
{
    List<string[]> rows = new List<string[]>();
    int[] indexes = { 0, 1, 3, 5, 6, 7, 8, 9 };
    using (var reader = new StreamReader(@"sourcedata.data"))
    {
        using (StreamWriter writetext = new StreamWriter("result.data"))
        {
            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                if (line.IndexOf(',') == -1)
                    continue;

                string[] values = line.Split(',');
                string[] row = new string[indexes.Length];
                int insertIndex = 0;
                for (int i = 0; i < values.Length; i++)
                {
                    string val = values[i];

                    if (val.Trim() == "?")
                        goto BREAK;

                    if (indexes.Contains(i))
                        row[insertIndex++] = val;
                }
                rows.Add(row);
                writetext.WriteLine(String.Join(",", row));
                BREAK:;
            }
        }
    }
}
Morteza Asadi
  • 1,819
  • 2
  • 22
  • 39
Alex
  • 15
  • 5
  • you need to run it in the background. google for background worker, or learn how to start threads directly – pm100 Aug 04 '17 at 15:29
  • what should the threads do ? can you guide me to useful references ? – Alex Aug 04 '17 at 15:41

2 Answers2

0

You have two split your problem into two separated sub problems:

  1. Write a method public static string DownloadData(...) which will download the data from the source. This can be done by any HTTP client or library you can find like System.Net.Http.HttpClient or System.Net.WebClient.

  2. Add/start a timer which calls this method periodically. You can use classes like System.Windows.Forms.Timer or System.Timers.Timer.

Progman
  • 16,827
  • 6
  • 33
  • 48
  • are there any direct waya to read the stream data directly from the website source periodically – Alex Aug 05 '17 at 12:53
  • @Alex As I wrote it above, you have to write one code which downloads the content and another code which calls this method. There is no `DownloadDataPeriodicallyFromAServer(url, time)` method. – Progman Aug 06 '17 at 07:38
  • I have implemented a function to download files successfully, but the csv files from the website can't be downloaded! – Alex Aug 06 '17 at 08:52
  • https://data.sparkfun.com/output/mKNlA0vdjAiyJ1p31XEM.csv this is a link for example .. in the browser it is being downloaded – Alex Aug 06 '17 at 08:53
  • @Alex When the browser can download it then your code can download it as well. Keep in mind that you need to limit the server response as described [in the documentation of phant.io](http://phant.io/docs/output/http/#paging). In your case the URL to use would be https://data.sparkfun.com/output/mKNlA0vdjAiyJ1p31XEM.csv?page=1. – Progman Aug 06 '17 at 09:05
  • please see my next answer – Alex Aug 06 '17 at 10:18
0

@Progman It is the code

 public partial class Download : Form
{
    public Download()
    {
        InitializeComponent();
    }

    WebClient client;
    private void btnDownload_Click(object sender, EventArgs e)
    {
        string url = txtUrl.Text;
        if (!string.IsNullOrEmpty(url))
        {
            Thread thread = new Thread(() =>
          {
              Uri uri = new Uri(url);
              string filename = System.IO.Path.GetFileName(uri.AbsolutePath);
              client.DownloadFileAsync(uri, Application.StartupPath + "/" + filename);

          });
            thread.Start();
        }

    }

    private void Download_Load(object sender, EventArgs e)
    {
        client = new WebClient();
        client.DownloadProgressChanged += Client_DownloadProgressChanged;
        client.DownloadFileCompleted += Client_DownloadFileCompleted;
    }

    private void Client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
    {
        MessageBox.Show("Download Completed.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);

    }

    private void Client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        Invoke(new MethodInvoker(delegate ()
        {
            progressBar.Minimum = 0;
            double recieve = double.Parse(e.BytesReceived.ToString());
            double total = double.Parse(e.TotalBytesToReceive.ToString());
            double percentage = recieve / total * 100;
            lblStatus.Text = $"Download {string.Format("{0:0.##}", percentage)}%";
            progressBar.Value = int.Parse(Math.Truncate(percentage).ToString());
        }));
    }
}
Alex
  • 15
  • 5