/// <summary> /// Execute in batch mode the enqueued tasks. /// </summary> /// <param name="viewModelType"> /// The view model type. /// </param> /// <exception cref="InvalidOperationException"> /// If the batch is already committed and the execution is in progress or committing via async way. /// </exception> /// <exception cref="System.ArgumentException"> /// The <paramref name="viewModelType"/> is not of type <see cref="IProgressNotifyableViewModel"/>. /// </exception> public void Commit(Type viewModelType = null) { if (viewModelType != null) { Argument.IsOfType("viewModelType", viewModelType, typeof(IProgressNotifyableViewModel)); } lock (_syncObj) { if (IsCommitting || IsRunning) { throw new InvalidOperationException(ExecutionIsInProgressErrorMessage); } if (_tasks.Count == 0) { throw new InvalidOperationException(AtLeastOneTaskShouldBeRegisteredErrorMessage); } _viewModelType = viewModelType; if (_viewModelType != null) { _progressNotifyableViewModel = (IProgressNotifyableViewModel)_viewModelFactory.CreateViewModel(_viewModelType, null); _uiVisualizerService.Show(_progressNotifyableViewModel); } else { _progressNotifyableViewModel = null; } IsCommitting = true; } Execute(); }
/// <summary> /// Create an implementation of the <see cref="IProgressNotifyableViewModel" />. /// </summary> /// <param name="viewModelType">The view model type.</param> /// <returns>The instance of <paramref name="viewModelType" />.</returns> private IProgressNotifyableViewModel TryCreateProgressNotifyableViewModelFrom(Type viewModelType) { IProgressNotifyableViewModel viewModel = null; if (viewModelType != null) { Argument.IsOfType(() => viewModelType, typeof(IProgressNotifyableViewModel)); viewModel = (IProgressNotifyableViewModel)_viewModelFactory.CreateViewModel(viewModelType, null); } return(viewModel); }
/// <summary> /// Execute in batch mode the enqueued tasks using specific view model instance. /// </summary> /// <param name="viewModel">The view model instance.</param> /// <param name="show">Indicates whether the view model will be shown.</param> /// <param name="asycn">Indicates whether the commit will be executed in asynchronous way or not.</param> /// <param name="completedCallback">The completed callback.</param> private void CommitUsingViewModel(IProgressNotifyableViewModel viewModel, bool show = true, bool asycn = false, Action completedCallback = null) { BeginCommit(() => viewModel, show); if (asycn) { _thread = new Thread(Execute); #if !SILVERLIGHT _thread.SetApartmentState(ApartmentState.STA); #endif _completedCallback = completedCallback; _thread.Start(); } else { Execute(); } }
/// <summary> /// Execute in batch mode the enqueued tasks asynchronously. /// </summary> /// <param name="completedCallback"> /// The completed callback. /// </param> /// <param name="viewModelType"> /// The vie model type. /// </param> /// <exception cref="InvalidOperationException"> /// If the batch is already committed and the execution is in progress or committing via async way. /// </exception> /// <exception cref="System.ArgumentException"> /// The <paramref name="viewModelType"/> is not of type <see cref="IProgressNotifyableViewModel"/>. /// </exception> public void CommitAsync(Action completedCallback = null, Type viewModelType = null) { if (viewModelType != null) { Argument.IsOfType("viewModelType", viewModelType, typeof(IProgressNotifyableViewModel)); } lock (_syncObj) { if (IsCommitting || IsRunning) { throw new InvalidOperationException(ExecutionIsInProgressErrorMessage); } if (_tasks.Count == 0) { throw new InvalidOperationException(AtLeastOneTaskShouldBeRegisteredErrorMessage); } _viewModelType = viewModelType; if (_viewModelType != null) { _progressNotifyableViewModel = (IProgressNotifyableViewModel)_viewModelFactory.CreateViewModel(_viewModelType, null); _uiVisualizerService.Show(_progressNotifyableViewModel); } else { _progressNotifyableViewModel = null; } _thread = new Thread(() => { // NOTE: Patch for delay a bit the thread start ThreadHelper.Sleep(100); Execute(); }); _thread.SetApartmentState(ApartmentState.STA); _completedCallback = completedCallback; IsCommitting = true; } _thread.Start(); }
/// <summary> /// Execute in batch mode the enqueued tasks using specific view model instance. /// </summary> /// <param name="viewModel">The view model instance.</param> /// <param name="show">Indicates whether the view model will be shown.</param> /// <param name="asycn">Indicates whether the commit will be executed in asynchronous way or not.</param> /// <param name="completedCallback">The completed callback.</param> private void CommitUsingViewModel(IProgressNotifyableViewModel viewModel, bool show = true, bool asycn = false, Action completedCallback = null) { BeginCommit(() => viewModel, show); if (asycn) { _thread = new Thread(() => { bool initialized = false; do { _dispatcherService.Invoke(() => initialized = Application.Current.MainWindow != null); Thread.Sleep(100); }while (!initialized); Execute(); }); #if !SILVERLIGHT _thread.SetApartmentState(ApartmentState.STA); #endif _completedCallback = completedCallback; _thread.Start(); } else { if (!Dispatcher.CheckAccess()) { throw new NotSupportedException("This method must be executed in non-UI thread. Please try with CommitAsync."); } if (Application.Current.MainWindow == null) { throw new NotSupportedException("The application is not completly initialized. Please try with CommitAsync."); } Execute(); } }
/// <summary> /// Verifies the state of the service and also sets the commiting state to <c>true</c>. /// </summary> /// <param name="viewModelFunc">The view model instance.</param> /// <param name="show">Indicates whether the view model will be shown. If the view model is <c>null</c> then this argument will be ignored.</param> /// <exception cref="System.InvalidOperationException"> /// </exception> /// <exception cref="InvalidOperationException">If the batch is already committed and the execution is in progress or committing via async way.</exception> private void BeginCommit(Func <IProgressNotifyableViewModel> viewModelFunc = null, bool show = true) { lock (_syncObj) { if (IsCommitting || IsRunning) { throw new InvalidOperationException(ExecutionIsInProgressErrorMessage); } if (_tasks.Count == 0) { throw new InvalidOperationException(AtLeastOneTaskShouldBeRegisteredErrorMessage); } _progressNotifyableViewModel = viewModelFunc == null ? null : viewModelFunc.Invoke(); if (_progressNotifyableViewModel != null && show) { _dispatcherService.Invoke(() => _uiVisualizerService.Show(_progressNotifyableViewModel)); } IsCommitting = true; } }