/** * 任务完成 */ private static void OnUploadCompleted(string message, bool bSuccess) { InternalPenddingFiles.Remove(UploadInfo.CurrentFile); InternalScannedFiles.Add(UploadInfo.CurrentFile); InternalSuccessList.Add(UploadInfo.CurrentFile); InternalSuccessFiles.Add(UploadInfo.CurrentFile, message); // 创建失败记录 if (!bSuccess) { Log.WriteRecord(UploadInfo.CurrentFile + " | " + message); } // 删除压缩临时文件 if (UploadInfo.CurrentFile != UploadInfo.TempFile && File.Exists(UploadInfo.TempFile)) { File.Delete(UploadInfo.TempFile); } UploadInfo = null; if (UploadClient != null) { UploadClient.Dispose(); UploadClient = null; } }
/** * 扫描检查 */ private static void ScanThreadMethod() { while (RunScanThread) { Option.ShareMutex.WaitOne(); var ScannDirectorys = new List <string>(); foreach (var item in Config.Base.ScannDirectorys) { ScannDirectorys.Add(item); } var ServerAddress = Config.Base.ServerAddress; Option.ShareMutex.ReleaseMutex(); foreach (var item in ScannDirectorys) { ScanCatalogue(item, true); } if (UserInfo != null && UploadClient == null && InternalPenddingFiles.Count > 0) { // 更新上传信息 if (UploadInfo == null) { string currentFile = InternalPenddingFiles.First().Key; string tempFile = currentFile; try { // 图片压缩处理 CompressedPicture(currentFile, out tempFile); } catch { Log.WriteRecord(currentFile + " | 图片压缩失败"); } UploadInfo = new UploadInfo(currentFile, tempFile); } // 发起上传请求,监听进度和完成回调 if (File.Exists(UploadInfo.TempFile)) { try { Uri uri = new Uri(ServerAddress + "/upload/scenic?scenic_id=&user_id=" + UserInfo.user_id); UploadClient = new WebClient(); UploadClient.Credentials = CredentialCache.DefaultCredentials; UploadClient.UploadFileAsync(uri, "POST", UploadInfo.TempFile); UploadClient.UploadProgressChanged += new UploadProgressChangedEventHandler(OnUploadProgressChangedEventHandler); UploadClient.UploadFileCompleted += new UploadFileCompletedEventHandler(OnUploadFileCompleted); } catch (Exception ex) { OnUploadCompleted(ex.Message, false); } } else { OnUploadCompleted("上传失败,文件不存在", false); } } Option.ShareMutex.WaitOne(); PenddingFiles.Clear(); foreach (var item in InternalPenddingFiles) { PenddingFiles.Add(item.Key, item.Value); } SuccessFiles.Clear(); while (InternalSuccessList.Count > 100) { string key = InternalSuccessList[0]; InternalSuccessList.Remove(key); InternalSuccessFiles.Remove(key); } foreach (var item in InternalSuccessFiles) { SuccessFiles.Add(item.Key, item.Value); } Option.ShareMutex.ReleaseMutex(); Thread.Sleep(1000); } }