FTP多任务下载实现类
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; using System.Net; using System.IO; namespace RSGIS.FTPClient { public partial class FormLoad : DevExpress.XtraEditors.XtraForm { private string host = "ftp://...:21"; private string user = "anonymous"; private string pass = "..."; private string strLocalFilePath = @"D:"; private string[] strLists; private List<MultiFtpService> MultiThread = new List<MultiFtpService>(); public FormLoad(string[] strList) { InitializeComponent(); strLists = strList; } public FormLoad() { InitializeComponent(); strLists = new string[3]; strLists[0] = “” strLists[1] = “” strLists[2] = “” private void buttonStart_Click(object sender, EventArgs e) { taskDo(); } private void buttonStop_Click(object sender, EventArgs e) { stop(); } private void buttonContinues_Click(object sender, EventArgs e) { continues(); } //停止 private void stop() { for (int i = 0; i < MultiThread.Count; i++) { MultiThread[i].Stop(); } } //继续 private void continues() { for (int i = 0; i < MultiThread.Count; i++) { MultiThread[i].continues(); } } //下载 private void taskDo() { for (int i = 0; i < strLists.Length; i++) { string[] files = fileName(strLists[i], strLists[i].Substring(strLists[i].LastIndexOf("/") + 1)); MultiFtpService threads = new MultiFtpService(host, user, pass, i, i, files[0], files[1]); MultiThread.Add(threads); threads.WorkMethod += DisplayProgress; threads.FileSpeedMethod += DisplaySpeed; threads.Start(); } } //路径操作 private string[] fileName(string strRemoteFilePath, string strLocalFileName) { string strLocalFilePaths = strLocalFilePath + @"" + strLocalFileName; strLocalFilePaths.Replace("\\", "\"); strRemoteFilePath.Replace("/", "\"); string[] files = { strRemoteFilePath, strLocalFilePaths }; return files; } /// <summary> /// 进度显示 /// </summary> /// <param name="threadindex"></param> /// <param name="taskindex"></param> /// <param name="progress"></param> private void DisplayProgress(int taskindex, int progress) { this.BeginInvoke(new MethodInvoker(delegate() { label1.Text = taskindex.ToString(); this.progressBar2.Value = progress; if(progress == 100) this.progressBar2.Value = progressBar2.Maximum; })); } //下载速度显示 private void DisplaySpeed(int taskindex, string speed) { this.BeginInvoke(new MethodInvoker(delegate() { this.labelSpeed.Text = speed + "/s"; })); } } }