我从 REST 服务上传大量图像,并通过定期任务使用它们创建各种 gif 图像。我想知道如何优化我的代码,改进它并使其更快。 public class WebcamListViewModel : BaseViewModel{ public ICommand InitializeWebcamsCommand { set; get; } public ICommand OpenVideoWebcamCommand { set; get; }private List<Webcam> _ListOfWebcam { get; set; }public List<Webcam> ListOfWebcam{ get { return _ListOfWebcam; } set { _ListOfWebcam = value; OnPropertyChanged(); }}private IFolder folder;private int _Counter { get; set; }public int Counter{ get { return _Counter; } set { _Counter = value; OnPropertyChanged(); }}private Task SetFrameOnViewTask;private Task DownloadFramesTask;CancellationTokenSource tokenSourceSetFrame = new CancellationTokenSource();CancellationTokenSource tokenSourceDownloadFrames = new CancellationTokenSource();CancellationToken cancellationTokenSetFrame;CancellationToken cancellationTokenDownloadFrames;public WebcamListViewModel(INavigationService navigationService, IApiAutostradeManagerFactory apiAutostradeManagerFactory) : base(navigationService,apiAutostradeManagerFactory){ OpenVideoWebcamCommand = new Command<Webcam>(async (webcam) => { await navigationService.NavigateAsync(Locator.WebcamVideoPopUpPage); Messenger.Default.Send(new InfoWebcamVideoMessage(webcam.c_mpr, webcam.c_uuid, webcam.t_str_vid)); }); InitializeWebcamsCommand = new Command(async () => await RunSafe(InitializeWebcams())); InitializeWebcamsCommand.Execute(null); cancellationTokenDownloadFrames = tokenSourceDownloadFrames.Token; DownloadFramesTask = new Task(async () => { cancellationTokenDownloadFrames.ThrowIfCancellationRequested();在我的 viewModel 中,我有两个任务:DownloadFramesTask 和 SetFrameOnViewTask,每 500 毫秒增加一个计数器,用于显示轮流中的四个帧之一。
1 回答
波斯汪
TA贡献1811条经验 获得超4个赞
而不是这个:
foreach (var web in ListOfWebcam)
{
web.image1 = await GetWebcamFrame(web.frame1);
web.image2 = await GetWebcamFrame(web.frame2);
web.image3 = await GetWebcamFrame(web.frame3);
web.image4 = await GetWebcamFrame(web.frame4);
}
您可以一次执行所有异步任务:
var tasks = new List<Task>();
tasks.Add(GetWebcamFrame(web.frame1));
// add more tasks here
await Task.WhenAll(tasks);
Task.Delay()您还可以删除对;的使用。目前还不清楚为什么有必要这样做。
- 1 回答
- 0 关注
- 67 浏览
添加回答
举报
0/150
提交
取消