我正在尝试实现一些文件(视频文件)来生成他们的媒体信息。我有一个名为(Details)的列表视图,每个文件根据它们的类型添加3-4次,如下所示:
动作复仇女神无限战争2.6 GB
冒险复联无限战争2.6 GB
科幻电影“复联无限战争”2.6 GB
行动任务不可能失败4.7 GB
冒险任务不可能失败4.7 GB
惊险片《不可能的任务》4.7 GB
冒险玩具总动员1.4 GB
喜剧玩具总动员1.4 GB
我试着实现了几个这样的代码
Parallel.For(0, Details.Items.Count, i =>
{
FileName = Details.Items[i].SubItems[7].Text;
Stop.Start();
new Methods.Generate(FileName, FileHost, Sender as BackgroundWorker);
});并使用normal for循环
for (int Indices = 0; Indices < Details.Items.Count; Indices++)
{
FileName = Details.Items[Indices].SubItems[7].Text;
Thread Generating = new Thread(() => new Methods.Generate(FileName, FileHost, Sender as BackgroundWorker))
{
IsBackground = true
};
Generating.Start();
Generating.Join();
}我希望输出处理只有2个电影在同一时间,没有类型的特定电影的限制。
像复仇女神无限战争和任务不可能失败这样的例子,一开始就会生成。在复仇者联盟的无限战争或任务不可能的失败完成上传其媒体信息后,我想开始第三个第一个玩具总动员4,以此类推。(2在当前时间,当1完成时,再添加1)
生成方法:
class Generate
{
public Generate(string FileName, string FileHost, BackgroundWorker BW)
{
Random Rand = new Random();
Thread.Sleep(1000);
BW.ReportProgress(Rand.Next(0, 40), FileHost + " " + new FileInfo(FileName).Name);
Thread.Sleep(2000);
BW.ReportProgress(Rand.Next(40, 70), FileHost + " " + new FileInfo(FileName).Name);
Thread.Sleep(3000);
BW.ReportProgress(Rand.Next(70, 100), FileHost + " " + new FileInfo(FileName).Name);
}
}其他:
private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
MessageBox.Show(e.UserState.ToString());
}发布于 2019-07-04 10:59:33
您可以使用接受ParallelOptions参数的Parallel.For重载。它允许配置并行度。
var options = new ParallelOptions() { MaxDegreeOfParallelism = 2 };
Parallel.For(0, Details.Items.Count, options, i =>
{
FileName = Details.Items[i].SubItems[7].Text;
Stop.Start();
new Methods.Generate(FileName, FileHost, Sender as BackgroundWorker);
});发布于 2019-07-04 11:28:14
var options = new ParallelOptions();
options.MaxDegreeOfParallelism = 2;
Parallel.For(0, Details.Items.Count, options, i =>
{
FileName = Details.Items[i].SubItems[7].Text;
Stop.Start();
new Methods.Generate(FileName, FileHost, Sender as BackgroundWorker);
});发布于 2019-07-05 08:21:03
我删除了paralle.for,现在我使用..for循环,它工作得非常好,除了我只能一个接一个地处理。这也是可行的。
for (int Indices = 0; Indices < Details.Items.Count; Indices++)
{
FileName = Details.Items[Indices].SubItems[7].Text;
Stop.Start();
Thread Generating = new Thread(() => new Methods.Generate(FileName, FileHost, Sender as BackgroundWorker))
{
IsBackground = true
};
Generating.Start();
Generating.Join();
}https://stackoverflow.com/questions/56879978
复制相似问题