You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
3.3 KiB
102 lines
3.3 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.ComponentModel;
|
|
using connor_zwcadm.util;
|
|
|
|
namespace connor_zwcadm.dialog {
|
|
public class KBackgroundWorker {
|
|
public BackgroundWorker backgroundWorker { get; }
|
|
private Window parent;
|
|
public KProgressBar pbDialog;
|
|
public string CompleteMessage { get; set; }
|
|
|
|
public KBackgroundWorker(string completeMessage=null, Window parent = null) {
|
|
this.parent = parent;
|
|
this.CompleteMessage = completeMessage;
|
|
//可以返回工作进度
|
|
backgroundWorker = new BackgroundWorker();
|
|
backgroundWorker.WorkerReportsProgress = true;
|
|
//允许取消
|
|
backgroundWorker.WorkerSupportsCancellation = true;
|
|
backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted;
|
|
//更新进度条
|
|
backgroundWorker.ProgressChanged += backgroundWorker_ProgressChanged;
|
|
}
|
|
|
|
public void Start(object param, bool disableCancel = true) {
|
|
if (backgroundWorker.IsBusy) {
|
|
return;
|
|
}
|
|
if (parent != null) {
|
|
parent.IsEnabled = false;
|
|
}
|
|
pbDialog = new KProgressBar(backgroundWorker);
|
|
if (disableCancel) {
|
|
pbDialog.tb_cancel.Width = 0;
|
|
pbDialog.tb_cancel.Visibility = Visibility.Hidden;
|
|
}
|
|
backgroundWorker.RunWorkerAsync(param);
|
|
}
|
|
|
|
public bool IsCancel() {
|
|
return backgroundWorker.CancellationPending;
|
|
}
|
|
|
|
|
|
private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
|
|
pbDialog.Dispatcher.Invoke(new Action(delegate {
|
|
pbDialog.Close();
|
|
}));
|
|
if (parent != null) {
|
|
parent.IsEnabled = true;
|
|
}
|
|
if (e.Error != null) {
|
|
KUtil.LogErr(e.Error);
|
|
ZwSoft.ZwCAD.ApplicationServices.Application.ShowAlertDialog(e.Error.Message);
|
|
}
|
|
else {
|
|
if (e.Cancelled) {
|
|
ZwSoft.ZwCAD.ApplicationServices.Application.ShowAlertDialog("已取消");
|
|
}
|
|
else if(!string.IsNullOrWhiteSpace(CompleteMessage)) {
|
|
ZwSoft.ZwCAD.ApplicationServices.Application.ShowAlertDialog(CompleteMessage);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) {
|
|
pbDialog.Dispatcher.Invoke(new Action(delegate {
|
|
pbDialog.progressBar.Value = e.ProgressPercentage;
|
|
}));
|
|
}
|
|
|
|
public void Progress(string text, string detail, int progress) {
|
|
pbDialog.Dispatcher.Invoke(new Action(delegate {
|
|
pbDialog.l_MainProgress.Content = text;
|
|
pbDialog.l_SubProcess.Content = detail;
|
|
}));
|
|
if (progress >= 0) {
|
|
backgroundWorker.ReportProgress(progress);
|
|
}
|
|
}
|
|
|
|
public void Progress(string text, int progress) {
|
|
Progress(text, text, progress);
|
|
}
|
|
|
|
public void ProgressDetail(string text) {
|
|
pbDialog.Dispatcher.Invoke(new Action(delegate {
|
|
pbDialog.l_SubProcess.Content = text;
|
|
}));
|
|
}
|
|
|
|
public void Progress(string text, int index, int limit) {
|
|
Progress(text, (index + 1) * 100 / limit);
|
|
}
|
|
}
|
|
}
|