C#想对多个文件使用任务并行库吗

C#想对多个文件使用任务并行库吗,c#,C#,我想传输多个文件,但我使用了backgroundworker。但它只能处理一个文件。我听说过任务并行库。那么如何在我的代码中使用它呢。还是其他更好的方法? 这是我的密码 表格1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; us

我想传输多个文件,但我使用了backgroundworker。但它只能处理一个文件。我听说过任务并行库。那么如何在我的代码中使用它呢。还是其他更好的方法? 这是我的密码

表格1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.IO;
using System.Threading;

namespace Sender2
{

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Browse_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            txtSelectFilePath.Text = openFileDialog1.FileName;
            String path1=txtSelectFilePath.Text;
            files_list.Items.Add(path1);
            files_list.View = View.List;

        }
    }

    private void Send_Click(object sender, EventArgs e)
    {
        TransferService2.TransferService2Client client = new TransferService2.TransferService2Client();
        foreach (ListViewItem item in files_list.Items)
        {
            TransferService2.File file = client.DownloadDocument(item.Text);
            backgroundWorker1.RunWorkerAsync(item.Text);

        } 
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        TransferService2.TransferService2Client client = new TransferService2.TransferService2Client(); 
        string newpath = (string)e.Argument;
            TransferService2.File file = client.DownloadDocument(newpath);
            FileStream fs = new FileStream(@"c:\DownloadedFiles\" + file.Name, FileMode.Create); 
            int pos = 0;
            int length = 128;
            while (pos < file.Content.Length)
            {
                if (length > (file.Content.Length - pos))
                {
                    length = file.Content.Length - pos;
                }
                fs.Write(file.Content, pos, length); 
                int progress_percentage = (int)(((double)pos / (double)file.Content.Length) * 100)+1;
                backgroundWorker1.ReportProgress(progress_percentage);


                pos = pos + length;
            }


    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
        label_percent.Text = e.ProgressPercentage.ToString()+"%";

    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        MessageBox.Show("Successful");
    }

}
}
ITransferService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace TransferService2
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "ITransferService2" in both code and config file together.
[ServiceContract]
public interface ITransferService2
{
    [OperationContract]
    File DownloadDocument(String filepath);
}

[DataContract]
public class File
{
    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public byte[] Content { get; set; }

}
}

Thanx提前了很多………

这很简单。只需将您的
foreach
替换为
Parallel.foreach
并设置集合和当前项-您还可以使用
MaxDegreeOfParallelism
等设置所使用线程的限制(一次运行的线程数)。这非常灵活

更多文档请参见

以下是一个示例:

Parallel.ForEach(files_list.Items, (item) =>
        {
            TransferService2.File file = client.DownloadDocument(item.Text);
            backgroundWorker1.RunWorkerAsync(item.Text);

        });

@Steve我想对多个文件使用任务并行库。ihv为您的快速rpl使用了后台处理。意味着我可以使用backgroundworker?不需要移除它吗?@ShankyRedkar测试一下,看看。如果它不起作用,则将该逻辑提取到另一个类中相当简单。先生,我尝试了此操作,但出现了错误-此BackgroundWorker当前正忙,无法同时运行多个任务。@ShankyRedkar然后提取该逻辑。您可以将其提取到一个类中,并为该操作启动多个线程。
Parallel.ForEach(files_list.Items, (item) =>
        {
            TransferService2.File file = client.DownloadDocument(item.Text);
            backgroundWorker1.RunWorkerAsync(item.Text);

        });