private void btnStart_Click(object sender, EventArgs e) { var b = sender as Button; if (b.Text == "Отмена") { if (this.BWorker != null && BWorker.IsBusy) { b.Enabled = false; pbProgress.Style = ProgressBarStyle.Marquee; this.BWorker.CancelAsync(); } } else { try { ProcessorParameters param = GetProcessorParameters(); if (param.Equals(this.PrevFinishedParameters)) { const string mess = "Вы хотите начать загрузку с теми же параметрами, как выполнили до этого. Вероятно, результат будет такой же.\n\nВы хотите продолжить?"; if (MessageBox.Show(mess, "Повторить?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } } b.Text = "Отмена"; BeginUploading(param); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
public void RunProcess(object sender, DoWorkEventArgs e) { this.BW = sender as BackgroundWorker; this.Parameters = e.Argument as ProcessorParameters; //Loading Items from excel file List <PictureItem> items; using (UsingExcel xls = new UsingExcel()) { items = xls.GetPhotoItems(this.Parameters.ExcelInfo, this.BW); } if (items == null || items.Count == 0) { throw new Exception("Нет записей в списке"); } UploaderBuilder builder = new Uploaders.UploaderBuilder(); builder.RootFolder = System.IO.Path.GetDirectoryName(this.Parameters.ExcelInfo.WorkBook.Path); builder.UploadFolderName = this.Parameters.UploadFolderName; builder.UploadDirection = this.Parameters.Direction; var uploader = builder.Build(); uploader.RunUpload(items, BW); using (UsingExcel xls = new UsingExcel()) { xls.UpdatePhotoItems(items, this.Parameters.ExcelInfo, BW); } e.Result = items.Count; }
private ProcessorParameters GetProcessorParameters() { if (this.OpenedExcelFile == null) { throw new Exception("Файл не загружен"); } Office.ExcelSheet selectedSheet = OpenedExcelFile.Sheets.FirstOrDefault(a => a.Index == Convert.ToInt32(cmbSheets.SelectedValue)); string columnWithNames = checkbUseNames.Checked ? cmbNames.Text : null; string columnWithLinks = cmbLinks.Text; int rowBegin = Convert.ToInt32(txtBeginRow.Text); int rowEnd = Convert.ToInt32(txtEndRow.Text); if (rowBegin < 1 || rowBegin > selectedSheet.LastCell.Row || rowEnd < rowBegin || rowEnd > selectedSheet.LastCell.Row) { throw new Exception("Указан неверный диапазон строк"); } UploadingExcelParameters excel = new Office.UploadingExcelParameters(OpenedExcelFile.Path, selectedSheet.Index, columnWithNames, columnWithLinks, rowBegin, rowEnd); excel.IncludeLinkToCell = this.chkLinkToCell.Checked; ProcessorParameters param = new ProcessorParameters(excel); param.UploadDirectory = txtPictureFolderName.Text.Trim(); param.UploadDirection = rbSaveLocal.Checked ? UploadDirection.LOCAL : UploadDirection.FTP; return(param); }
private void btnStart_Click(object sender, EventArgs e) { try { if (this.OpenedExcelFile == null) { throw new Exception("Необходимо открыть файл Excel."); } ExcelWorkSpaceInfo excelInfo = new Office.ExcelWorkSpaceInfo(this.OpenedExcelFile, (int)cmbSheets.SelectedValue); excelInfo.ColumnPictureNames = cmbNames.Text; excelInfo.ColumnPictureHyperlinks = cmbLinks.Text; excelInfo.RowBeginUpload = Convert.ToInt32(txtBeginRow.Text); excelInfo.RowEndUpload = Convert.ToInt32(txtEndRow.Text); var param = new ProcessorParameters { ExcelInfo = excelInfo, Direction = rbSaveLocal.Checked ? UploadPicturesDirection.LOCAL : UploadPicturesDirection.FTP, UploadFolderName = string.IsNullOrEmpty(txtPictureFolderName.Text) ? Guid.NewGuid().ToString() : txtPictureFolderName.Text }; SetControlsEnabled(false); this.BWorker = new BackgroundWorker(); this.Processor = new Processor(); BWorker.WorkerReportsProgress = true; BWorker.WorkerSupportsCancellation = false; BWorker.ProgressChanged += BWorker_ProgressChanged; BWorker.RunWorkerCompleted += BWorker_RunWorkerCompleted; BWorker.DoWork += this.Processor.RunProcess; BWorker.RunWorkerAsync(param); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void BeginUploading(ProcessorParameters param) { SetControlsEnabled(false); this.BWorker = new BackgroundWorker(); var processor = new Processor(); BWorker.WorkerReportsProgress = true; BWorker.WorkerSupportsCancellation = true; BWorker.ProgressChanged += BWorker_ProgressChanged; BWorker.RunWorkerCompleted += BWorker_RunWorkerCompleted; BWorker.DoWork += processor.RunProcess; groupBox4.Enabled = true; this.PrevFinishedParameters = null; BWorker.RunWorkerAsync(param); }
private void BWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Error != null) { ShowErrorMessage(e.Error, "Ошибка выполнения"); } else if (e.Cancelled) { lblStatus.Text = $"Отменено."; } else { lblStatus.Text = "Обработка файла завершена"; this.PrevFinishedParameters = e.Result as ProcessorParameters; } System.Diagnostics.Process.Start(this.OpenedExcelFile.Path); SetControlsEnabled(true); btnStart.Text = "Начать"; btnStart.Enabled = true; pbProgress.Style = ProgressBarStyle.Continuous; }
public override bool Equals(object obj) { if (obj == null) { return(false); } if (object.ReferenceEquals(this, obj)) { return(true); } ProcessorParameters other = obj as ProcessorParameters; if (other == null) { return(false); } return(this.ExcelInfo.Equals(other.ExcelInfo) && this.UploadDirection == other.UploadDirection && this.UploadDirectory == other.UploadDirectory && this.ImageResizeSettings.Equals(other.ImageResizeSettings)); }
public void RunProcess(object sender, DoWorkEventArgs e) { this.BW = sender as BackgroundWorker; this.Parameters = e.Argument as ProcessorParameters; //Loading Items from excel file List <PictureItem> items; BW.ReportProgress(0, "Считываем таблицу..."); using (UsingExcel xls = new UsingExcel()) { items = xls.GetPhotoItems(this.Parameters.ExcelInfo); } if (items == null || items.Count == 0) { throw new Exception("Нет записей в списке"); } string uploadDir = string.IsNullOrEmpty(Parameters.UploadDirectory) ? Guid.NewGuid().ToString() : Parameters.UploadDirectory; if (Parameters.UploadDirection == Uploaders.UploadDirection.LOCAL) { uploadDir = System.IO.Path.GetDirectoryName(Parameters.ExcelInfo.FilePath) + "\\" + uploadDir; } Uploaders.UploaderBuilder builder = new Uploaders.UploaderBuilder(uploadDir); Uploaders.IUploader uploader = builder.Build(Parameters.UploadDirection); uploader.Initialize(); BW.ReportProgress(0, "Сохранение фото..."); ProgressTicker ticker = new ProgressTicker(items.Count, 1); ticker.ProgressChanged += Ticker_ProgressChanged; List <PictureItem> uploadedItems = new List <PicturesUploader.PictureItem>(); foreach (var item in items) { if (BW.CancellationPending) { e.Cancel = true; break; } if (item.Error == null) { try { ImageResizer.ImageInfo image = BuildImage(item.Address); image.ResizeImage(this.Parameters.ImageResizeSettings); string pictureName = $"{item.Name}.{image.SourceExtention}"; item.Address = uploader.SaveImage(image.DestinationBitmap, pictureName); } catch (ImageResizer.ImageCorruptedException) { item.Error = new Exception("Не прямая ссылка на изображение, либо файл поврежден"); } //catch(System.Net.WebException wex) //{ // item.Error = wex; //} catch (Exception ex) { item.Error = ex; } } uploadedItems.Add(item); ticker.Tick(); } using (UsingExcel xls = new UsingExcel()) { BW.ReportProgress(0, "Записываем результат в Excel файл"); xls.UpdatePhotoItems(uploadedItems, this.Parameters.ExcelInfo); } e.Result = items.Count; }