Datetime:2016-08-23 01:25:58         Topic: MVVM Model  WPF          Share        Original >>
Here to See The Original Article!!!


Task-based Asynchronous Pattern (TAP) is pattern to executing of asynchronous operations. In this project go to perform operations that do not return results ( Task ) and return result ( Task <TResult> ). It's the MVVM pattern used to orchestrate the execution of the task and also build a control to present the progress of task and subtask.



The following link is a document explaining task concepts: Exceptions, cancellation token, progress reports, etc:


When the project start was WPF client, but as the future of this technology is uncertain decided to use a Windows client, so the class that orchestrates the execution of processes inherited from INotifyPropertyChanged, according to the WPF development. The project contains well-known implementations of commands (IRelayCommand: ICommand). The objective of this standard was to link the properties and behavior to controls client. Binding works fine in WPF, but Windows applications did not work and had to be other strategies to present data in a progress form.


Progress is presented in a control Windows divided in 3 regions:

  1. Task Progress: This panel shows the task name, current item and execution time of each task item.

  2. Progress subtask: The data presented are the same as the previous panel.

  3. Command buttons: Allow Start, pause, cancel the execution or close the window.


Here they are explained the projects that make up the solution: Asimatica.TaskAsyn and how to implement a new solution.


The project Asimatica.FW.MvvmTask , implement MVVM pattern, the class is the most important: AbstractViewModel,.

public abstract class AbstractViewModel : INotifyPropertyChanged, IDisposable

public abstract class AbstractViewModel : INotifyPropertyChanged, IDisposable


The project Asimatica.FW.AsynTask , is a library that performs a task asynchronously, the most important classes are:

Interface ITaskManagement

Defines the contract to run a task asynchronously.

public interface ITaskManagement<TArgument, TResult, TProgress>

Class TaskManagement

Implements the interface ITaskManagement, the heart is at properties that store the method having the business rules to be executed asynchronously, the method may or not have executing parameter and can return or no result:

Func<ITaskManagement<TArgument, TResult, TProgress>, TArgument, Task> DoTaskAsync
Func<ITaskManagement<TArgument, TResult, TProgress>, Task> DoTaskWithouParameterAsync
Func<ITaskManagement<TArgument, TResult, TProgress>, TArgument, Task<TResult>> DoTaskResultAsync
Func<ITaskManagement<TArgument, TResult, TProgress>, Task<TResult>> DoTaskResultWithouParameterAsync

Methods exist to perform two types of tasks: they do not return anything and returning value. The task can have a series of actions by example 30 days of the month and every item can throw a subtask cycle, which in turn can have its own cycle of progress, so will find members Task and Subtask

public Task RunAsync(TArgument parameter)
    DateTime dtStart;
    if (IsBusy)
        return null;
    dtStart = DateTime.Now;
    var task = DoTaskAsync(this, parameter);
    if (this.Scheduler == null)
        task.ContinueWith(t => TaskCompleted(task, dtStart));
        task.ContinueWith(t => TaskCompleted(task, dtStart), this.Scheduler);
    return task;

When executing a task or subtask, ends either successfully, with failure or canceled by the user, it is executed one method: TaskCompleted or TaskCompletedResult, these methods execute callback methods stored in properties, which are set by the class that uses this library.

The start and end time are stored, as well as the time of each item of the task or subtask. Controlling these times showed difficult, because the processes are asynchronous.

Progress is controlled by three kinds of methods: Configure task, start progress report item task or subtask and report end progress of item task or subtask; these methods in turn launch callback methods to notify the change.

To define the callback methods should execute the following methods:

  • SetMethodsCallback
  • SetSubTaskMethodsCallback

Class TaskProgressViewModel

Controls the execution of tasks and provide support executing progress notifications. This class launches the method having the business rules and sends the progress. For the visual part, it was created control and Windows form in the project: Asimatica.FW.AsynTaskWin . The WPF form of project: Asimatica.FW.AsynTaskWpf was the initial prototype, but is incomplete.


TaskProgressWindow control and frmTaskProgressWindow form

The TaskProgressWindow control, presented visually progress data.



The project: Asimatica.FW.AsynTask.WinTest , have five tasks, which they can be executed simultaneously.

Data model

File: ProcessModel.cs contains two models and a mock class to generate test data:

Services with business rules

Task Services.cs file contains classes and methods with the business rules, methods must have the signature of one of the properties of the interface: ITaskManagement : DoTaskAsync, DoTaskWithouParameterAsync, DoTaskResultAsync, DoTaskResultWithouParameterAsync.


Class: MainTestViewModel , defines viewmodel to the main form. The class contains data models, business rules tasks (services) and TaskProgressViewModel objects for managing the execution and progress.

frmMain form

The form execute five task define to services

  • Test 1: Open the form: frmTaskProgressWindow, to execute task.

  • Test 2: Open other form: frmTaskProgressWindow, to execute task.

  • Test 3: Control progress is incorporated into form.

  • Test 4: The task is executed without using visual components.

  • Test 5: The task does not use the built visual component; it uses a progress bar into form client, for submitting progress visually.


The project: Asimatica.FW.AsynTask.WpfTest , It was the initial prototype and is incomplete.


  • Run long-running tasks asynchronously using the TAP pattern.

  • Present to user with a progress indicator and data such as start and total time of each item. Use the form or add to the progress control within a form to show progress.


Document Task-based Asynchronous Pattern (TAP)

Task Parallel Library

WinForms and TPL - Achieving quick Multitasking and Responsive User Interface

BackgroundWorker - Aplicación VS 2005, con ejemplo para WinForms



Version 1.0.0.


Put your ads here, just $200 per month.