WPF: Using Task Parallel Library to Load Data
Posted by: Mahesh Sabnis
in Category WPF
Abstract: In this article, I have described a scenario where a WPF application is trying to use the task parallel library to load data. The app retrieves data using a WCF service and also accesses data from the local database server. The application dedicates a long running WCF service call to the Task class, so that the call to the service can be made asynchronously.
In .NET 4.0, we have a set of new API's to simplify the process of adding parallelism and concurrency to applications. This set of API's is called the "Task Parallel Library (TPL)" and is located in the System.Threading and System.Threading.Tasks namespaces. In the Table shown below, I have mentioned some of the classes used for Parallel programming in .NET 4.0.
If you are new to Parallel Tasks in .NET, check this article Introduction to .NET Parallel Task
In this article, I have described a scenario where a WPF application is trying to retrieve data using a WCF service and also trying to access data from the local database server. The application dedicates a long running WCF service call to the Task class, so that the call to the service can be made asynchronously. The below diagram explains the scenario:
Step 1: Open VS2010 and create a blank solution, name it as ‘CS_Task_Demo’. In this project, add a new WCF Service application project and name it as ‘WCF40_DataService’. Rename IService1.cs to IService.cs and Service1.svc to Service.svc.
Step 2: Write the following code in IService.cs with ServiceContract, OperationContract and DataContract. The OperationContract method also applies the WebGet attribute so that the WCF service is published as WCF REST service.
Step 3 : Now write the following code in the Service class. This code makes a call to Database and retrieve the Employee records.
Note: The above code uses the Thread.Sleep(10000) which waits for 10 seconds to get the data from the Database. I did this to emulate the effect of the Task class.
Step 4: Change the code of Service.svc as shown below:
Step 5: Make the following changes in Web.Config file which adds the WebHttpBinding for the WCF REST Services.
<add binding="webHttpBinding" scheme="http"/>
Step 6: Publish the WCF service on IIS.
Step 7: In the same solution, add a new WPF project and name it as ‘WPF40_TasksClass’,make sure that the framework for the project as .NET 4.0. Add the following XAML:
Step 8: Open MainPage.xaml.cs and add the below classes:
Step 9: Add the following code in the GetData click event. This code defines a Task object which initiates an Asynchronous operation to the WCF REST Service. This downloads the XML data by making call to WCF service. During this time of the service call, a call to the local database is made and completes the call. Once the service call is over, the data is processed. The code is as below:
The above code creates an instance of the Task class using its Factory property to retrieve a TaskFactory instance that can be used to create task.
Note: Please read comments in the code carefully to understand it.
Step 10: Run the application and click on the ‘Get Data’ button. You will get the data immediately in the DataGrid. This data is fetched from the local sql server database as below:
Click on the ‘OK’ for the message box and wait for some time, you will get data in the DataGrid on the Left hand side which gets the data from the WCF REST service as shown below:
Now if you see the above output, the time for the WCF service call is more than 10 Seconds and the local database call takes 0.2 Seconds.
The entire source code of this article can be downloaded over here