DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. JavaScript
  4. C# 4.0 - Beginners look into parallel programming

C# 4.0 - Beginners look into parallel programming

Richard Mccutchen user avatar by
Richard Mccutchen
·
May. 18, 10 · Interview
Like (0)
Save
Tweet
Share
38.79K Views

Join the DZone community and get the full member experience.

Join For Free
When C# 4.0 was released we C# developers were given a new toy. to play with, and that's Parallel Programming. This is done with the System.Threading.Tasks.Parallel Namespace. This allows for parallel loops & regions (to be discussed at a later date). What is parallel programming you ask, well in todays age most, if not all, household computers have multi-core processors, and parallel programming allows us to take davantage of this new found power, well not actually new as multi-core processors have been around a while now, but software has not kept up with the changes in hardware.

Used to we had to use multi-threading for something that resembled synchronously operations. This was the norm for programming, but things are changing and evolving and Microsoft has finally given us the ability to take advantage of multi-core processors on the systems we're programming for. Now to take advantage of this you will need the .Net 4.0 Framework and Visual Studio 2010. Before we go on I will let you know that there are extensions (Reactive Extensions for .Net) that allow you to access this from C# 3.5 but for the purpose of this article we're referring to 4.0 only.

NOTE: In all honesty you should only use parallel when you have a long, intensive process. If you use it for small, easy loops & tasks you could actuall slow down the overall process.

First think we want to do is create ourself a new Visual Studio 2010 project (I'm using a console application for this sample) so get that open and create your new project. Now we want to add a reference to the System.Threading.Tasks Namespace, to do this go Project > Add Reference and select System.Threading.  Then in your code file add
using System.Threading.Tasks;
This gives us access to what we need for parallel work. Noe, for this introduction we're going to count the number of files in a given directory, including the subdirectories in said directory. We're going to store all the directories in a Stack<string> just for simplicity sake. For those who have never used the Stack this is a LIFO (last-in-first-out) collection, in this case of type string.

For traversing the directories in a parallel fashion we'll use Parallel.Foreach Method, which executes a for each operation in a parallel iteration. We will use the overloaded version
 Parallel.ForEach(TSource, TLocal) Method (Partitioner(TSource), ParallelOptions, Func(TLocal), Func(TSource, ParallelLoopState, TLocal, TLocal), Action(TLocal))
During the parallel loop we will also use Interlock.Exchange that way we can keep the proper counter variable across multiple threads (atomic method). So here is how the method looks
public static List<string> GetAllFilesWithParallel(string root, Action<string> action)
{
//generic list for holding all the files
List<string> list = new List<string>();

//Count of files traversed
int i = 0;

//stack to hold all the file names
Stack<string> dirs = new Stack<string>();
try
{

//first make sure the directory provided actually exists
if (!System.IO.Directory.Exists(root))
{
Console.WriteLine(string.Format("The specified directory {0} could not be found", root));
return null;
}
//insert the root directory onto our stack (for later use)
dirs.Push(root);

//now we start looping
while (dirs.Count > 0)
{


//variables needed for this task
string currentDirectory = dirs.Pop();
string[] subDirectories = null;
string[] files = null;


//first get the subdirectories in the specified directory
subDirectories = System.IO.Directory.GetDirectories(currentDirectory);
//now get all the files in the current directory
files = System.IO.Directory.GetFiles(currentDirectory);
//add all the subdirectories onto the stack for traversal
foreach (string str in subDirectories)
dirs.Push(str);

//now use the Parallel.ForEach loop to get all the files in the current directory
//and add them to our generic list
Parallel.ForEach(files, () => 0, (file, loopState, localCount) =>
{
action(file);
return (int)++localCount;
},
(c) =>
{
Interlocked.Exchange(ref i, i + c);
});
}
}
catch (AggregateException e)
{
e.Handle((ex) =>
{
if (ex is UnauthorizedAccessException)
{
// Here we just output a message and go on.
Console.WriteLine(ex.Message);
return true;
}
return false;
});
}
return list;
}

To test our method we will do this in our main method. Here we will pass it the directory we wish to search and our Action. In this case our action will be adding the file names to our Generic list. We will prompt for the directory and get the files and count them. which looks like this

static void Main(string[] args)
{
List<string> files = new List<string>();
Console.WriteLine("Please provide the directory you would like to count:");
string root = Console.ReadLine();

Console.WriteLine("Counting files .....");

GetAllFilesWithParallel(root, (f) => { files.Add(f); });

if(files.Count != 0)
Console.WriteLine(string.Format("Total files in {0}", files.Count()));

// Keep the console window open.
Console.ReadKey();
}

Notice we pass the method an Action, a delegate than encapsulates a method with no parameters. We go this route so we dont have to explicitly declare a delegate. So from this beginners look you can see that parallel programming is a powerful tool offered from Microsoft, and can finally allow us as programmers to keep up (somewhat anyways) with the ever changing hardware world and performance they're offering.

So this is a beginners look at the new parallel programming ability in C# 4.0. Next we will take a more intermediate look at parallel programming and how it can increase performance in your applications.

Directory application Pass (software) Advantage (cryptography) Console (video game CLI) Strings Testing

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Master Spring Boot 3 With GraalVM Native Image
  • Required Knowledge To Pass AWS Certified Solutions Architect — Professional Exam
  • Using GPT-3 in Our Applications
  • Public Key and Private Key Pairs: Know the Technical Difference

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: