/// <summary> /// 执行计时操作 /// </summary> /// <param name="target"></param> void Exec()//object target) { if (CheckState) { lock (CheckObj) { if (CheckState) { CheckState = false; Task.Factory.StartNew(() => { try { if (UploadFileList.Count > 0) { int count = ThreadsCount - UploadFileList.Where(c => c.Value.RunState == Entity.FTPRunState.Run && c.Value.CloseState == false).Select(c => c.Key).Count(); //检查并发数目是否超出限制 FTPItem item = null; if (count > 0) { //等待并发ID string[] keys = UploadFileList.Where(c => c.Value.RunState == Entity.FTPRunState.None && c.Value.CloseState == false).Select(c => c.Key).Take(count).ToArray(); if (keys != null && keys.Length > 0) { foreach (var key in keys) { if (UploadFileList.TryGetValue(key, out item)) { //item = UploadFileList[key]; //if (item.CloseState == false && item.RunState == Entity.FTPRunState.None) { item.RunState = FTPRunState.Run; Task.Factory.StartNew(FTPExec, key); } } } } } } } catch (Exception ex) { Console.WriteLine(ex); } CheckState = true; }); } } } }
/// <summary> /// FTP操作 /// </summary> /// <param name="part"></param> void FTPExec(object target) { try { string key = Convert.ToString(target); FTPItem part = null; if (UploadFileList.TryGetValue(key, out part)) { if (part.IsUpload) //上传 { if (part.IsContinue == false) //新上传的文件需检查目录 { string ftp = string.Concat("ftp://", IP, ":", Port); string[] path = part.ServerFilePath.ToLower().Split('/'); if (path.Length > 4)//一级级创建FTP目录 { int len = path.Length - 1; for (int i = 3; i < len; i++) { ftp = string.Concat(ftp, "/", path[i]);//检查目录 if (CheckDir(ftp, FTPUser, FTPPassword) == false) { //创建目录 CreateDir(ftp, FTPUser, FTPPassword); } } } } UploadFTPFile(key, part); } else//下载 { DownloadFTPFile(key, part, GetFtpFileSize(part.ServerFilePath, FTPUser, FTPPassword)); } } Exec(); } catch (Exception ex) { Console.WriteLine(ex); } }