C#希望对多个文件使用任务并行库
本文关键字:任务 并行 文件 希望 | 更新日期: 2023-09-27 18:27:24
我想传输多个文件,但我使用了backgroundworker。但它只能处理一个文件。我听说过任务并行库。那么如何在我的代码中使用它。或者其他更好的方法?这是我的密码。。。
Form1.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");
}
}
}
TransferService2.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace TransferService2
{
public class TransferService2 : ITransferService2
{
public File DownloadDocument(String filepath)
{
File file = new File();
String path = filepath;
byte[] buffer;
FileStream fs = new FileStream(@path, FileMode.Open, FileAccess.Read);
try
{
int length = (int)fs.Length;
buffer = new byte[length];
int count;
int sum = 0;
while((count=fs.Read(buffer,sum,length-sum))>0)
{
sum = sum + count;
}
}
finally
{
fs.Close();
}
file.Content = buffer;
file.Name = Path.GetFileName(path);
return file;
}
}
}
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
等设置使用线程的限制(一次运行多少个)。这非常灵活。
MSDN 上的更多文档
这是一个示例:
Parallel.ForEach(files_list.Items, (item) =>
{
TransferService2.File file = client.DownloadDocument(item.Text);
backgroundWorker1.RunWorkerAsync(item.Text);
});