0

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

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

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 特定文件完成时,我如何从任务中实时知道特定文件完成和另一个文件开始?

4

1 回答 1

0

我无法使用编译器对此进行测试,而且我有一段时间没有做过 WinForms,所以这里可能会有一些小的语法错误,但这应该会让你朝着正确的方向前进:

... 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;
}
于 2013-07-30T14:33:25.563 回答