Data Parallelism in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ParallelInvokeMethods
{
class DataParallelism
{
static void Main(string[] args)
{
SynchronousProcess();
}
private static void PostData()
{
// Code to send data to web API ( web service ).
Thread.Sleep(60000);
}
private static void SynchronousProcess()
{
Console.WriteLine("Syncronus Process Started");
DateTime startTime = DateTime.Now;
List<int> batchList = new List<int> { 1, 2, 3, 4, 5 };
Console.WriteLine("Start Time:" + startTime);
foreach (int item in batchList)
{
Console.WriteLine("Begin task..." + item);
PostData();
Console.WriteLine("End task..." + item);
}
DateTime endTime = DateTime.Now;
Console.WriteLine("End Time:" + endTime);
TimeSpan timeExecution = endTime - startTime;
Console.WriteLine("Total time execution :" + timeExecution);
Console.WriteLine("Syncronus Process End");
}
}
}
Synchronous Process Started
Start Time:5/30/2015 12:49:29 PM
Begin task...1
End task...1
Begin task...2
End task...2
Begin task...3
End task...3
Begin task...4
End task...4
Begin task...5
End task...5
End Time:5/30/2015 12:54:29 PM
Total time execution :00:05:00.0333292
Synchronous Process End
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ParallelInvokeMethods
{
class DataParallelism
{
static void Main(string[] args)
{
ParallelProcess();
}
private static void PostData()
{
// Code to send data to web API ( web service ).
Thread.Sleep(60000);
}
private static void ParallelProcess()
{
Console.WriteLine("Parallel Process Started");
DateTime startTime = DateTime.Now;
List<int> batchList = new List<int> { 1, 2, 3, 4, 5 };
Console.WriteLine("Start Time:" + startTime);
Parallel.ForEach(batchList, item =>
{
Console.WriteLine("Begin task..." + item);
PostData();
Console.WriteLine("End task..." + item);
});
DateTime endTime = DateTime.Now;
Console.WriteLine("End Time:" + endTime);
TimeSpan timeExecution = endTime - startTime;
Console.WriteLine("Total time execution :" + timeExecution);
Console.WriteLine("Parallel Process End");
}
}
}
Write a comment
- Lavon April 29, 2016, 7:28 pmI simply want to mention I am beginner to blogging and sieitbuild-ng and certainly liked your blog site. Almost certainly i want to bookmark your website . You amazingly come with exceptional posts. Many thanks for sharing with us your blog site.reply
- Arco September 21, 2015, 5:10 amArticle is really fruitful for me, keep up posting such articles or reviews.reply
- Jeema June 15, 2015, 5:19 pmGreat blog and I look forward to seeing new more!!reply
- Aarti June 15, 2015, 11:51 amKeep posting such great blogsreply