INTRODUCTION
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.
PATTERN AND PROGRESS
TASK - ASYNCHRONOUS
The following link is a document explaining task concepts: Exceptions, cancellation token, progress reports, etc:
http://www.microsoft.com/en-us/download/details.aspx?id=19957
PATTERN MVVM
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.
TASK PROGRESS
Progress is presented in a control Windows divided in 3 regions:
-
Task Progress: This panel shows the task name, current item and execution time of each task item.
-
Progress subtask: The data presented are the same as the previous panel.
-
Command buttons: Allow Start, pause, cancel the execution or close the window.
USING THE CODE
Here they are explained the projects that make up the solution: Asimatica.TaskAsyn and how to implement a new solution.
PROJECT MVVM PATTERN
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
TASK PROJECT
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;
InitilizeRun(dtStart);
var task = DoTaskAsync(this, parameter);
if (this.Scheduler == null)
{
task.ContinueWith(t => TaskCompleted(task, dtStart));
}
else
{
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.
PROJECT TASK PROGRESS
TaskProgressWindow control and frmTaskProgressWindow form
The TaskProgressWindow control, presented visually progress data.
CUSTOMER APPLICATION
CUSTOMER WINDOWS
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.
ViewModel
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.
CUSTOMER WPF
The project: Asimatica.FW.AsynTask.WpfTest, It was the initial prototype and is incomplete.
POINTS OF INTEREST
LINKS
Document Task-based Asynchronous Pattern (TAP)
http://www.microsoft.com/en-us/download/details.aspx?id=19957
Task Parallel Library
http://www.codeproject.com/Articles/152765/Task-Parallel-Library-of-n
WinForms and TPL - Achieving quick Multitasking and Responsive User Interface
http://www.codeproject.com/Articles/748815/WinForms-and-TPL-Achieving-quick-Multitasking-and
BackgroundWorker - AplicaciĆ³n VS 2005, con ejemplo para WinForms
http://www.codeproject.com/Articles/42103/Generic-Background-Worker
Tools
http://wftoolkit.codeplex.com/
http://mvvmlight.codeplex.com/
http://MVVMhelpers.codeplex.com/
HISTORY
Version 1.0.0.