提供多线程或任务创建

时间:2019-07-04 02:36:37

标签: c# multithreading

我正在尝试实现一些文件(视频文件)以生成其媒体信息。我得到一个名为(详细信息)的列表视图,每个文件根据其类型分别添加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);
                        });

并使用普通的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());
}

3 个答案:

答案 0 :(得分:0)

您可以使用接受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);
});

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我删除了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();
                        }
相关问题