简体   繁体   English

UWP - 如何启动并行运行的多个任务?

[英]UWP - How to start multiple tasks running in parallel?

I have an app where I select multiple (300-500) images then create a video where each image is a frame.我有一个应用程序,其中我 select 多个(300-500)图像然后创建一个视频,其中每个图像都是一个帧。 In order to load these images into a composition for playback, I need to convert them and save them to temporary memory before they can be loaded.为了将这些图像加载到合成中进行播放,我需要将它们转换并保存到临时 memory 才能加载它们。 This conversion/save process is taking some time and I'm trying to find ways to speed up the task.这个转换/保存过程需要一些时间,我正试图找到加快任务的方法。

What would be a good way to have multiple instances of this conversion task take place in parallel?让这个转换任务的多个实例并行发生的好方法是什么? Currently, CPU usage is low (4%-6%) while the conversion is taking place.目前,在进行转换时,CPU 使用率很低 (4%-6%)。

Here is how I'd like it to work:这是我希望它的工作方式:

  1. User picks multiple image files用户选择多个图像文件

  2. A folder in temporary storage is created在临时存储中创建一个文件夹

  3. Multiple image convert/save tasks are created and run in parallel.创建多个图像转换/保存任务并并行运行。

  4. After conversion and save is complete, all converted images in temporary storage folder are selected and loaded into a composition for playback.转换保存完成后,将临时存储文件夹中的所有转换后的图像都选中并加载到合成中进行播放。

Here's what I have now:这是我现在拥有的:

private async Task OpenImageSequence(IReadOnlyList<StorageFile> files)
{

    destFolder = await ApplicationData.Current.TemporaryFolder.CreateFolderAsync(NameNoExt, CreationCollisionOption.GenerateUniqueName);
    var composition = new MediaComposition();

    foreach (StorageFile file in files)
    {
        CreateImage(file);
    }

    IReadOnlyList<StorageFile> fileList = await destFolder.GetFilesAsync();

    foreach (StorageFile imgFile in fileList)
    {
        var clip = await MediaClip.CreateFromImageFileAsync(imgFile, TimeSpan.FromSeconds(frameIncrement));
        composition.Clips.Add(clip);
    }

    MediaStreamSource newImageSequence = composition.GenerateMediaStreamSource(defaultProfile);
    windowsSource = MediaSource.CreateFromMediaStreamSource(newImageSequence);

}

private async Task CreateImage(StorageFile file)
{
    var stream = await file.OpenStreamForReadAsync();
    
    //....Image Conversion Code....//

    var destFile = await destFolder.CreateFileAsync("exrConvert.png", Windows.Storage.CreationCollisionOption.GenerateUniqueName);

    await canvas.SaveAsync(destFile.Path.ToString(), CanvasBitmapFileFormat.Png);
}

Basically I'd like to have as many instances of the the CreateImage task running in parallel as possible.基本上我希望有尽可能多的 CreateImage 任务实例并行运行。 Any thoughts as to how I go about doing this?关于我如何 go 这样做的任何想法?

I tried the following for a selection of 10 images but the images did not seem to be processed in parallel:我尝试了以下 10 张图像的选择,但图像似乎没有被并行处理:

    Task t1 = CreateImage(files.ElementAt(0));
    Task t2 = CreateImage(files.ElementAt(1));
    Task t3 = CreateImage(files.ElementAt(2));
    Task t4 = CreateImage(files.ElementAt(3));
    Task t5 = CreateImage(files.ElementAt(4));
    Task t6 = CreateImage(files.ElementAt(5));
    Task t7 = CreateImage(files.ElementAt(6));
    Task t8 = CreateImage(files.ElementAt(7));
    Task t9 = CreateImage(files.ElementAt(8));
    Task t10 = CreateImage(files.ElementAt(9));

    await Task.WhenAll(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10);

Thanks for any help you can provide!感谢您的任何帮助,您可以提供!

To let multiple tasks run in parallel, each task have to be composed as a background task.为了让多个任务并行运行,每个任务都必须组合为后台任务。 So you need to use Task.Run() or something.所以你需要使用Task.Run()什么的。 Plain async function with Task return type does not yield background task.具有任务返回类型的普通异步 function 不会产生后台任务。

//foreach (StorageFile file in files)
//{
//    await CreateImage(file);
//}
var tasks_in_background_thread = files.Select(file => Task.Run(async () =>
{
    await CreateImage(file);
}));
await Task.WhenAll(tasks_in_background_thread);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM