0
我有一个播放文件的应用程序,每个文件都有它自己的运行时间,我只需将我的文件添加到我的Listview
中,然后单击播放按钮。任务Parallel.ForEach在工作中报告
这是我的函数接受列表作为输入,这个名单包括我的所有文件,因为我想要的选项来运行超过1个文件同时我可以控制的并行文件数:
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可以订阅的事件?每次任务完成时只需触发事件。只需确保在更新UI之前将'Invoke'调用到UI线程中。 – Pete
我可以举个例子吗? (我是一名新开发人员) – user2214609