任务Parallel.ForEach在工作中报告

时间:2013-07-30 14:14:15

标签: c# winforms

我有一个播放文件的应用程序,每个文件都有自己的运行时间,我只需将我的文件添加到Listview并单击播放按钮。

这是我的函数,它接收List作为输入,这个列表包括我的所有文件,因为我希望选项同时运行多个文件,我可以控制并行文件的数量:

public void doWork(IEnumerable<string> source, int parallelThreads )
{
    _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
        );
} 

当文件完成运行另一个文件开始播放(如果我选择同时播放多个文件)并且在我想要更新我的UI特定文件完成的情况下,我怎么能从Task中实时知道那个特定的文件文件完成和另一个文件启动?

1 个答案:

答案 0 :(得分:0)

我无法使用编译器对此进行测试,并且我暂时没有完成WinForms,所以这里可能会有一些小的语法错误,但这应该让你朝着正确的方向前进:< / p>

... In your form code ...

ClassNameHere myOb = new ClassNameHere();
myOb.FileComplete += new FileCompleteHandler(FileDone);

...

public void FileDone(object sender, EventArgs e)
{
    if (InvokeRequired)
    {
        Invoke(new FileCompleteHandler(FileDone), new object[] { sender, e });
        return;
    }

    ... update UI here ...
}

然后,在您的班级中执行实际工作(当然,您可以随意调用该班级。)

public delegate void FileCompleteHandler(object sender, EventArgs e)
public class ClassNameHere
{

    protected virtual void OnFileComplete()
    {
        FileCompleteHandler handler = FileComplete;
        if (handler != null)
        {
            handler(this, EventArgs.Empty);
        }
    }

    public void doWork(IEnumerable<string> source, int parallelThreads )
    {
        _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...

                        // This is where we tell the UI to update itself.
                        OnFileComplete();
                    });
            }
            catch (Exception)
            { }

        }, _tokenSource.Token).ContinueWith(
                t =>
                {
                    //finish...
                }
            , TaskScheduler.FromCurrentSynchronizationContext() //to ContinueWith (update UI) from UI thread
            );
    } 

    public event FileCompleteHandler FileComplete;
}