namespace Utils.Basis.ViewModel { using Autodesk.Revit.DB; using Utils.Basis; using System; using System.ComponentModel; public class ProgressViewModel : ViewModelBase { private BackgroundWorker _backgroundWorker = new BackgroundWorker(); private bool _canceled; private DelegateCommand _cancelProcess; private int _count; private ProgressViewModel.DoWorkDelegate _doWorkDelegate; private int _maxCount = 100; private bool _progressBarActive; private Transaction _transaction; private string _windowTitle = "Selecting Elements"; public ProgressViewModel(ProgressViewModel.DoWorkDelegate doWorkDelegate) { this._doWorkDelegate = doWorkDelegate; this._backgroundWorker.DoWork += this._backgroundWorker_DoWork; this._backgroundWorker.RunWorkerCompleted += this._backgroundWorker_RunWorkerCompleted; this._backgroundWorker.WorkerSupportsCancellation = true; this._cancelProcess = new DelegateCommand(new Action(this.Cancel)); } public delegate void DoWorkDelegate(); public bool Canceled { get { return this._canceled; } set { this._canceled = value; } } public DelegateCommand CancelProcess { get { return this._cancelProcess; } } public int Count { get { return this._count; } set { this._count = value; } } public int IntPercentage { get { int result; try { result = Convert.ToInt32(Math.Round((double)this.Count / (double)this.MaxCount * 100.0, 1)); } catch { result = 0; } return result; } } public bool IsBusy { get; set; } public int MaxCount { get { return this._maxCount; } set { this._maxCount = value; } } public bool ProgressBarActive { get { return this._progressBarActive; } set { this._progressBarActive = value; } } public ProgressViewModel.DoWorkDelegate SetDelegate { set { this._doWorkDelegate = value; } } public string StringPercentage { get { return this.IntPercentage.ToString() + "%"; } } public Transaction Transaction { get { return this._transaction; } set { this._transaction = value; } } public string WindowTitle { get { return this._windowTitle; } set { this._windowTitle = value; } } public void Cancel() { this.Canceled = true; this.IsBusy = false; this._backgroundWorker.CancelAsync(); if (this._transaction != null) { this._transaction.Dispose(); } } public void RunWorker() { this._backgroundWorker.RunWorkerAsync(); } private void _backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { this._doWorkDelegate(); } private void _backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { this.IsBusy = false; } } }