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

时间:2014-04-07 19:34:07

标签: c# .net task

我有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
        );
}

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

1 个答案:

答案 0 :(得分:0)

当然是:) 将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();
        }
    }
}