我在Windows窗体应用程序中实现了一个函数,用于从文件(sourcedata.data)捕获和读取所需的表格数据,并将其保存在另一个文件(result.data)中。 我和使用该应用程序如何捕获这样的实时流数据:csv或.data文件中的https://data.sparkfun.com/streams以使用它。 或者有没有直接的方法来定期直接从网站来源读取流数据?
[b -> c] -> [b] -> [c]
答案 0 :(得分:0)
你有两个将你的问题分成两个独立的子问题:
编写一个方法public static string DownloadData(...)
,它将从源代码下载数据。这可以通过您可以找到的任何HTTP客户端或库来完成,例如System.Net.Http.HttpClient或System.Net.WebClient。
添加/启动定时调用此方法的计时器。您可以使用System.Windows.Forms.Timer或System.Timers.Timer等类。
答案 1 :(得分:0)
@Progman 这是代码
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());
}));
}
}