WPF: Using Task Parallel Library to Load Data

Posted by: Mahesh Sabnis , on 8/24/2011, in Category WPF
Views: 79611
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.

image

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:

.NET Task Class

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.

image

Step 3 : Now write the following code in the Service class. This code makes a call to Database and retrieve the Employee records.

image

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:

WPF Parallel Task Service

Step 5: Make the following changes in Web.Config file which adds the WebHttpBinding for the WCF REST Services.

<protocolMapping>

<add binding="webHttpBinding" scheme="http"/>

</protocolMapping>

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:

WPF Parallel Task XAML

Step 8: Open MainPage.xaml.cs and add the below classes:

image

 

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:

WPF Parallel Task Data

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:

WPF Parallel Task Demo

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:

WPF Parallel Task Demo

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

Give a +1 to this article if you think it was well written. Thanks!
Recommended Articles
Mahesh is having 10 years of experience in IT education and development. He is a Microsoft Certified Trainer (MCT) since 2005 and has conducted various Corporate Training programs for .NET Technologies (all versions). Follow him on twitter @maheshdotnet


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by malak on Saturday, May 26, 2012 5:19 AM
how to fetch the data to my design use the JsonStore to store data and php to load..
<?php
   $conn =sqlserver_connect("localhost", "test1", "test1") or die (sqlserver_error ());
   $db = sqlserver_select_db ("Sample") or die (sqlserver_error ());

   $result=sqlserver_query ("SELECT * FROM strawhats") or die (sqlserver_error ());

   $data = array();

   while ($row=sqlserver_fetch_object($result))
   {
      $data [] = $row;
   }


   echo json_encode($data);
?>
i want to help how get data when use the php..
my sqlserver is >>>>>>

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
         conn= DriverManager.getConnection(
               "jdbc:sqlserver://localhost:1433;databaseName=Sample",
               username, password);

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel