Tag Archives: software development

C# Parallel Programming – Increment variable safely across multiple threads

On one of my recent projects, I utilized a bit of parallel programming via the C# Parallel.ForEach construct.  Within the foreach loop, I needed to log what current step in the process I was on, like so Step 1 of n.  So this means we need some sort of variable (let’s call it “x”) that can be incremented to indicate the step number.

Now, we could simply try to increment the variable (as seen below), however, the problem here is that you can have two threads operating concurrently on separate processors, but accessing the same variable.  This may cause  process on thread 1 to increment variable “x” from 0 to 1, but because the process is non atomic (other actions can be performed on the value at the same time), process on thread 2 can at the same time access the value of variable “x”, but still read the value as 0 as it hasn’t been saved yet.

int index = 0;
Parallel.ForEach(databaseNameList, (dbName) =>
var currIndex = index++;
Log(String.Format("Step {0} of {1}", currIndex, databasenameList.Count));

To get around this, we’ll need something that can safely change the value of a shared variable from multiple threads.  This can be accomplished using the lock statement, and you can find an example here.  However, for simpler and faster code, we will utilize the C# Interlocked class.  The Interlocked class has numerous members we can call, but in our case we are particularly interested in the Increment method.  What makes it great is that it performs the increment task in one atomic (meaning no other action can be performed on the value at the same time) operation.  So instead of the above piece of code, it changes slightly to the below code sample.

int index = 0;
Parallel.ForEach(databaseNameList, (dbName) =>
var currIndex = Interlocked.Increment(ref index);
Log(String.Format("Step {0} of {1}", currIndex, databasenameList.Count));

This is a post I’ve held on to and wanted to post for a long while now, so I know it may not be new information, but still hope this little tidbit is useful to someone else.

Tagged , , ,

It’s Alive!!!

Okay, maybe not so much as of late.  The last year (and then some) has been quite a ride personally and professionally.

My responsibilities at work have grown immensely since my last post, and I must say I have needed the time to adjust.  I have also been participating in quite a few software development competitions, some of which have yielded some good success (I’ll post about them soon).  Overall, it’s been a good year, but it’s time to get consistent again with my blogging.  Not so much a “New year’s resolution”, but more of a end-of-the-year resolution I guess.

When I started this blog, it was a way for me to further my learning and share the little bit of knowledge I pick up along the way.  That remains the goal.

And so, the journey continues!

Tagged ,

amCharts for HTML5/JavaScript


I have developed several web and mobile apps, some of which needed some sort of data visualization.  Among my arsenal of charting tools is amCharts.  So far, I have used their charts for Silverlight, WPF, and Windows Phone 7 platform-based applications, and they have always performed great.

With my recent dive into developing apps using HTML5/Javascript/JQuery, I needed to make sure I had a decent option for charting data.  And sure enough, with the recent release of amChart’s charting controls for Javascript/HTML5, I know I have a reliable option for charting.

Check out their website here for a closer look at all their tools and controls.

Tagged ,

Easy Diary for Windows Phone 7

I have always wanted to get into the mobile space.  Particularly with developing apps.  So when the Windows Phone 7 was announced, as a newbie Silverlight developer (a year ago), I thought this would be a great chance to do so.  And so, I dove in.

The Win Phone 7, a Silverlight & XNA developer platform makes it fairly easy for current Silverlight/XNA developers to transition into the mobile space.  And I have to say, it’s been very exciting.

Easy Diary, a diary/journal application, was my first (of many apps to come) for the Windows Phone 7.  It gives potential users a quick and easy way to record moments throughout their day.  They can also connect with popular social sites like Twitter & Facebook, and display their online activity as part of their diary.  The app took about two weeks of total work, and was released to the Windows Marketplace a few months ago prior to the release of the phone. 

I will soon be posting more info on my current and future apps on my site here.  In the meantime, you can click here to view my app in the marketplace (Zune software required).  There was also a small article posted about my app here.

While I would like to say I figured everything out on my own while developing my app, that just won’t be right.  So I have to give credit where credit is due.  See below for credit(s), resources, and screenshots of the app.


Laurent Bugnion created a very light, flexible and versatile MVVM framework for the Windows Phone 7 (dubbed MVVM Light), Silverlight, and WPF.  It made my life very easy while developing this app and it will come in very handy in the many apps to come.


There are many valuable resources pertaining to developing apps for the Windows Phone.  Ranging from beginner to expert levels.  Here are just a few of them:


There are currently over 3,000 apps in the windows marketplace, and it’s going to be very exciting to see many talented developers come up brilliant ideas and even more great apps in times to come.

Good move Microsoft…good move.

Tagged , , , , , , , ,
%d bloggers like this: