查找使用 Task Parallel.ForEach 时当前正在处理的文件

本文关键字:处理 文件 Task Parallel ForEach 查找 | 更新日期: 2023-09-27 17:57:07

Winforms应用程序,其中包含我的应用程序播放的文件列表。因为我想添加同时播放多个文件的选项,所以我添加了这个:

private IEnumerable<string> _source;
public void doWork()
{
    _tokenSource = new CancellationTokenSource();
    var token = _tokenSource.Token;
    Task.Factory.StartNew(() =>
    {
        try
        {
            Parallel.ForEach(_source,
                new ParallelOptions
                {
                    MaxDegreeOfParallelism = _parallelThreads //limit number of parallel threads 
                },
                file =>
                {
                    if (token.IsCancellationRequested)
                        return;
                    //do work...
                });
        }
        catch (Exception)
        { }
    }, _tokenSource.Token).ContinueWith(
            t =>
            {
                //finish...
            }
        , TaskScheduler.FromCurrentSynchronizationContext() //to ContinueWith (update UI) from UI thread
        );
}

在这种情况下,我可以播放多个文件,但因为我想知道当前正在处理哪些文件,所以我的问题是是否可以知道当前正在处理_source哪些文件

查找使用 Task Parallel.ForEach 时当前正在处理的文件

当然是:)将 richTextBox 和按钮放在窗体上进行测试。

这里最重要的是使用 InvokeIfRequired,它允许你在从正在创建的非 UI 线程访问 UI 线程控件时避免跨线程异常。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private IEnumerable<string> _source = new List<string>() { "one", "two", "three", "four", "five" };
        public void doWork()
        {
            CancellationTokenSource _tokenSource = new CancellationTokenSource();
            var token = _tokenSource.Token;
            Task.Factory.StartNew(() =>
            {
                try
                {
                    Parallel.ForEach(_source,
                        new ParallelOptions
                        {
                            MaxDegreeOfParallelism = 4 //limit number of parallel threads 
                        },
                        file =>
                        {
                            if (token.IsCancellationRequested)
                                return;
                            if (InvokeRequired)
                            {
                                Invoke((Action<string>)richTextBox1.AppendText, "Task no: " + Task.CurrentId + " processing file: " + file + Environment.NewLine);
                            }
                            else
                            {
                                richTextBox1.AppendText("Task no: " + Task.CurrentId + " processing file: " + file + Environment.NewLine);
                            }
                        });
                }
                catch (Exception)
                { }
            }, _tokenSource.Token).ContinueWith(
                    t =>
                    {
                        //finish...
                    }
                , TaskScheduler.FromCurrentSynchronizationContext() //to ContinueWith (update UI) from UI thread
                );
        }
        private void button1_Click(object sender, EventArgs e)
        {
            doWork();
        }
    }
}