在c#, WinForms中调用大数据到图表对象
本文关键字:数据 对象 调用 WinForms | 更新日期: 2023-09-27 18:03:49
我目前正在用c#写一个程序。我想保存大量的数据(超过20万倍的双值)到一个。csv文件。这意味着我必须使用分号";"分隔所有值。
这不是问题,但是当我想再次打开文件并读取数据时,应该将值绘制成图表。
我正在逐行读取.csv文件,因为首先读取所有数据会导致内存溢出异常。我想在一个单独的线程上做这个,这样表单就不会被阻塞。然后我还必须调用图表。
现在多次调用控件需要花费很多时间。我在互联网上发现了类似的问题,计时器被用来解决这个问题。我已经使用计时器而不是调用进度条,但以防万一,我不知道如何使用这个计时器。
这里可以有计时器吗?我该如何实现它?有没有别的解决办法?
你的问题不是很清楚,但至少有一条信息可以回答你问题的两个部分:
我想在一个单独的[sic]线程上做这个,这样表单就不会被阻塞。
…
有别的解决办法吗?
使用任务并行库,你可以明确地防止UI线程阻塞,而无需负担线程管理。
这也允许您在进度条中将执行这些任务所需的步骤显示为"步骤"。然后,您可以枚举步骤,并显示一个真实的进度条,而不是一个猜测的进度条。
protected void LoadData_Click(Object sender, EventArgs e)
{
// Execute a task without blocking the calling method
Task.Factory.StartNew(() => { LoadDataAsync(); });
// method finishes, does not block on the task
}
private async void LoadDataAsync()
{
// Await the load and use the remainder of this method as callback
UpdateProgressBar();
int position = 0;
int take = Constants.PreferredFetchQuantity;
int total = MoreThan200xDoubleValue;
while (CheckCancellationToken == false && position <= total)
{
var Data = await Task.Run(FetchDataRowsSlowly(position, take));
// the remainder is a nice readable callback
position += take;
UpdateProgressBar(position);
foreach(var row in Data) SomeGridOrSomething.Add(row);
// ...
}
}
private Task<DataTable> FetchDataRowsSlowly(int start, int count)
{
var db = WhateverDbContextOrWebAPICallOrWhatever();
// return datatable, the framework will handle the Task<>
return db.GiantTable.Skip(start).Take(count).ToDataTable();
}