FileStream异步读写文件,CPU超高!

weixin_38053534 2018-07-20 03:42:51
如图,WPF的DataGrid中添加了DataGridTemplateColumn,用于展示远程服务器的文件,点开始按钮后开始获取文件并下载到本地。
每个下载任务中采用异步Task,过程中发现开启单个或多个下载任务时都会占用CPU很高,如何优化?

后台:
public ObservableCollection<FileInfo> fileInfoCollection = new ObservableCollection<FileInfo>();
public void StartDownLoad(string sourceFilePath, string localFilePath, int fileIndex)
{
if (!fileInfoCollection[fileIndex].IsStart)
{
Task.Run( async () => { await DwonLoadFile(sourceFilePath, localFilePath, fileIndex); });
}
}
private async Task DwonLoadFile(string sourceFilePath, string localFilePath, int fileIndex)
{
if (!File.Exists(sourceFilePath))
return;
WebRequest request = WebRequest.Create(sourceFilePath);
WebResponse response = await request.GetResponseAsync();
Stream netStream = response.GetResponseStream();
Stream fileStream = new FileStream(localFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
fileStream.Position = 0;
byte[] read = new byte[1024];
long progressBarValue = 0;
int realReadLen = await netStream.ReadAsync(read, 0, read.Length);
Thread thread = new Thread(new ThreadStart(async delegate
{
while (realReadLen > 0 && !fileInfoCollection[fileIndex].IsStopped)
{
if (fileInfoCollection[fileIndex].IsSuspend == false)
{
await fileStream.WriteAsync(read, 0, realReadLen);
progressBarValue += realReadLen;
double len = double.Parse((progressBarValue / 1024.00 / 1024.00).ToString("F2"));
fileInfoCollection[fileIndex].CompletePercentage = Math.Ceiling(((double)progressBarValue / (double)netStream.Length) * 100) + "%";
fileInfoCollection[fileIndex].CompletedProgress = len;
fileInfoCollection[fileIndex].ProgressBarValue = progressBarValue;
realReadLen = await netStream.ReadAsync(read, 0, read.Length);
continue;
}
Thread.Sleep(1);
}
netStream.Close();
fileStream.Close();
}));
thread.IsBackground = true;
thread.Start();
}

前台:

private void SuspendOrStartBtn_Click(object sender, RoutedEventArgs e)
{
FileInfo item = ((Button)sender).DataContext as FileInfo;
int index = FileDataGrid.Items.IndexOf(item);

//开始任务
if (fileManager.fileInfoCollection[index].IsSuspend==true)
{
fileManager.StartDownLoad(item.SourceFileFullName, @"D:\" + item.SourceFileName, index);
fileManager.fileInfoCollection[index].IsStart = true;
fileManager.fileInfoCollection[index].IsSuspend = false;
SetPauseResumeButtonText(index);
}
else//暂停任务
{
fileManager.fileInfoCollection[index].IsSuspend = true;
SetPauseResumeButtonText(index);
}

}

...全文
31 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_38103381 2018-07-24
  • 打赏
  • 举报
回复
新创建的Task的数量应该与CPU的核数相当,比如有20个file download task,CPU 4核,则应该将task拆分为5个一组,只创建4个task。
专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms
weixin_38091211 2018-07-24
  • 打赏
  • 举报
回复
新创建的Task的数量应该与CPU的核数相当,比如有20个file download task,CPU 4核,则应该将task拆分为5个一组,只创建4个task。
专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms
weixin_38108704 2018-07-24
  • 打赏
  • 举报
回复

Hi 银光海阁,
一般情况下, 如果你使用异步下载文件, 他会自动检测到您当前可以使用的CPU, 然后尽可能的使用你剩余的CPU. 如果你觉得此操作占用CPU太高, 您可以考虑设置FileStream的Buffer来限流,这可能回降低当前文件下载所使用的CPU。

weixin_38063608 2018-07-23
  • 打赏
  • 举报
回复
Hi 银光海阁,
一般情况下, 如果你使用异步下载文件, 他会自动检测到您当前可以使用的CPU, 然后尽可能的使用你剩余的CPU. 如果你觉得此操作占用CPU太高, 您可以考虑设置FileStream的Buffer来限流,这可能回降低当前文件下载所使用的CPU。

weixin_38099114 2018-07-23
  • 打赏
  • 举报
回复
Hi 银光海阁,
一般情况下, 如果你使用异步下载文件, 他会自动检测到您当前可以使用的CPU, 然后尽可能的使用你剩余的CPU. 如果你觉得此操作占用CPU太高, 您可以考虑设置FileStream的Buffer来限流,这可能回降低当前文件下载所使用的CPU。

476

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧