我從 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 關注
- 91 瀏覽
添加回答
舉報
0/150
提交
取消