.Net 4.0 has come up with many new features. Recently I got in touch with an interesting feature that is Parallelism which is nothing but Parallel Execution of some task.
Parallelism can be achieved in two ways
1.     Parallel class
2.     PLINQ (Parallel LINQ)
I will be explaining both the techniques with an example that will have a list of websites in an string collection and we’ll try to ping the sites in parallel using the above two techniques. In the example I’ll be making use of the following namespace to access the Ping class.
C#
using System.Net.NetworkInformation;
 
VB.Net
Imports System.Net.NetworkInformation
 
Parallel class
The parallel class belongs to the System.Threading.Tasks Namespace. With the parallel class we execute any task parallel easily.
 
C#
List<string> sites = new List<string>
{
"www.aspsnippets.com",
"www.asp.net",
"www.microsoft.com",
"forums.asp.net",
"www.silverlight.net"
};
 
List<PingReply> pingReplies = new List<PingReply>();
System.Threading.Tasks.Parallel.ForEach(sites, site =>
{
    Ping p = new Ping();
    lock (pingReplies)
    {
        pingReplies.Add(p.Send(site));
    }
});
 
foreach (var s in pingReplies.ToList())
{
    Response.Write(s.Address + ": " + s.RoundtripTime + ": " + s.Status + "<br />");
}
 
VB.Net
Dim sites As New List(Of String)
sites.Add("www.aspsnippets.com")
sites.Add("www.asp.net")
sites.Add("www.microsoft.com")
sites.Add("forums.asp.net")
sites.Add("www.silverlight.net")
 
Dim pingReplies As New List(Of PingReply)()
System.Threading.Tasks.Parallel.ForEach(sites, Sub(site)
                                        Dim p As New Ping()
                                        SyncLock pingReplies
                                            pingReplies.Add(p.Send(site))
                                        End SyncLock
                                        End Sub)
 
 
For Each s In pingReplies.ToList()
       Response.Write(Convert.ToString(s.Address) & ": " & Convert.ToString(s.RoundtripTime) & ": " & Convert.ToString(s.Status) & "<br />")
Next
 
In the above example I am pinging the sites in parallel using Parallel Task class. I have also created another collection of Ping Replies and I am adding the ping reply of each site to that collection. You will notice I have put a lock on the collection as this is a parallel operation.
PLINQ (Parallel LINQ)
Next technique is very similar to above but belongs to a different namespace i.e. System.Linq
C#
List<string> sites = new List<string>
{
"www.aspsnippets.com",
"www.asp.net",
"www.microsoft.com",
"forums.asp.net",
"www.silverlight.net"
};
 
List<PingReply> pingReplies = (from site in sites.AsParallel().WithDegreeOfParallelism(sites.Count)
                           select DoPing(site)).ToList() as List<PingReply>;
 
foreach (var s in pingReplies.ToList())
{
    Response.Write(s.Address + ": " + s.RoundtripTime + ": " + s.Status + "<br />");
}
 
VB.Net
Dim sites As New List(Of String)
sites.Add("www.aspsnippets.com")
sites.Add("www.asp.net")
sites.Add("www.microsoft.com")
sites.Add("forums.asp.net")
sites.Add("www.silverlight.net")
 
Dim pingReplies As List(Of PingReply) = TryCast((
                  From site In sites.AsParallel().WithDegreeOfParallelism(sites.Count) _
                  Select DoPing(site)).ToList(), List(Of PingReply))
 
For Each s In pingReplies.ToList()
    Response.Write(Convert.ToString(s.Address) & ": " & Convert.ToString(s.RoundtripTime) & ": " & Convert.ToString(s.Status) & "<br />")
Next
 
Above you will notice that I am executing a LINQ query on the List of the sites. It looks like a normal LINQ query the only difference is that I have used the additional methods AsParallel (Tells the compiler to execute query in parallel) and WithDegreeOfParallelism (Allows you to specify the number of threads executed in parallel). Also I am calling a Method DoPing which basically pings the site and returns the Ping Reply, this method is described below
C#
private PingReply DoPing(string site)
{
    Ping p = new Ping();
    return p.Send(site);
}
 
VB.Net
Private Function DoPing(ByVal site As String) As PingReply
    Dim p As New Ping()
    Return p.Send(site)
End Function
 
With this we come to the end of this article. Hope you liked it.