19 using System.Collections.Generic;
24 using System.Threading.Tasks;
75 public bool IsProcessing
85 public bool WasCancelled
93 public bool WasSuccessful
101 public virtual Exception Exception {
get;
protected set; }
105 #region Public methods 114 Logger.Info(
"StartProcess: Starting process...");
115 _progressTimer =
new Timer(
118 Properties.Settings.Default.ShowProgressDelay,
119 Properties.Settings.Default.ShowProgressInterval);
130 Logger.Info(
"CancelProcess: CancelProcess was called!");
131 ProcessModel.Cancel();
136 #region MVVM messages 148 if (_showProgressMessage == null)
152 return _showProgressMessage;
163 if (_processFinishedMessage == null)
167 return _processFinishedMessage;
173 #region Abstract methods 194 ProcessModel = processModel;
199 #region Protected methods 209 Logger.Info(
"BeforeStartProcess");
220 Logger.Info(
"AfterStartProcess");
221 CloseViewCommand.Execute(null);
230 Logger.Info(
"SendProcessFinishedMessage: Sending message");
253 #region Protected properties 264 if (_processMessageContent == null)
266 Logger.Debug(
"Creating new ProcessMessageContent instance");
269 return _processMessageContent;
275 #region Private methods 286 private void Execute()
290 Logger.Fatal(
"Execute: Process is already running!");
291 throw new InvalidOperationException(
"Cannot start the process because it is already running");
293 if (!BeforeStartProcess())
return;
298 Logger.Info(
"Execute: Starting task");
299 Task.Factory.StartNew((Action)(() =>
307 Logger.Warn(
"Execute: Caught an exception!");
311 })).ContinueWith((task) => Dispatch(SendProcessFinishedMessage));
320 private void UpdateProgress(
object state)
322 lock (_lockUpdateProgress)
326 _showProgressWasSent =
true;
331 Logger.Info(
"UpdateProgress: The process is indeterminate");
333 Logger.Info(
"UpdateProgress: Sending ShowProgressMessage");
335 Logger.Debug(
"UpdateProgress: ... ShowProgressMessage was sent");
342 if (!IsIndeterminate)
345 Logger.Info(
"UpdateProgress: PercentCompleted: {0}",
351 Logger.Info(
"UpdateProgress: No longer processing, disposing update timer");
352 _progressTimer.Dispose();
360 #region Private fields 365 private Timer _progressTimer;
366 private bool _showProgressWasSent;
370 #region Private static fields 372 private static readonly
object _lockUpdateProgress =
new object();
378 private static NLog.Logger Logger {
get {
return _logger.Value; } }
380 private static readonly Lazy<
NLog.Logger> _logger =
new Lazy<
NLog.Logger>(() =>
NLog.LogManager.GetCurrentClassLogger());
Holds information about percent completion of a process and defines events that occur when the proces...
virtual void CancelProcess()
Cancels the ongoing process.
virtual void Send(T messageContent, Action< T > respond)
Calling this method will raise the Sent event with a message content and a callback method that can b...
virtual void StartProcess()
Entry point to starts the process.
virtual void AfterStartProcess()
Additional work to do after the process has started.
Abstract base class for view models that deal with processes.
Exception Exception
If something in the process went wrong, this will be the corresponding exception. ...
virtual void SendProcessFinishedMessage()
Sends the ProcessMessageContent.CompletedMessage to signal that the process has finished.
override object RevealModelObject()
Returns the associated Bovender.Mvvm.Models.ProcessModel (if any).
virtual bool BeforeStartProcess()
Additional work to do before the process is started.