c# Multi-threaded for loop

Posted on 2009-04-23
Medium Priority
Last Modified: 2013-12-17
I have a loop, which runs through and applies some templates to a powerpoint presentation.

I'm just beginning to learn how threads work, so this is obviously going to be an easy answer for most of you, but the problem is as follows.

The built-in interop function for applying the templates is time consuming with a lot of slides, so I wanted to break up that operation into multiple threads. Somehow, I don't know how, my loop skips the first iteration of i. The for loop, loops one more time than what it should. I don't know how it's skipping the first iteration.

Here's the code:
for(int i = 0; i < slides.length; i++)
     new System.Threading.Thread(delegate() { slides[i].applyTemplate(filename); }).Start();

Open in new window

Question by:Nitinol
  • 3
  • 2
LVL 15

Accepted Solution

oobayly earned 400 total points
ID: 24220408
Your entrance into the world of multithreading has resulted in a race condition.

What is happening is this:
  1. i = 0, Start 1st thread
  2. i = 1, Start 2nd thread
  3. 1st thread starts applying template, but now i = 1
This order is in no way guaranteed, this is just something you have to work with. To get around this, do it this way. I don't have VS on this machine, so even if the code doesn't compile, you should get the idea.

for(int i = 0; i < slides.length; i++)
  System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(ThreadedApplyTemplate));
  t.Start(new object[]{slides[i], filename});
private void ThreadedApplyTemplate(object state){
  SlideObject slide = (SlideObject)((object[])state)[0];
  string filename = (string)((object[])state)[1];

Open in new window


Author Comment

ID: 24220604
For learning purposes:

Can I assume that what's happening is that the ThreadedApplyTemplate method is assigning the slide object before i actually changes?

correct me if i'm wrong...
LVL 15

Expert Comment

ID: 24220782
In short yes.
What's happening is that you're passing a reference to the Slide object & the string to the ThreadedApplyTemplate method. Each thread is sure to have a different reference passed to the method.
Compare this to your initial code where all the threads were sharing a reference to the index integer, hence causing your race conditions.

Have a look at this article, I found it pretty good for helping with multithreading concepts, and it'll explain better than I ever can:

Author Comment

ID: 24221043
Thank you very much, I really appreciate your input. Perfect answer.

I usually end up having to solve my own problems, heh.

Author Closing Comment

ID: 31574014
Excellent feedback, thank you again.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Loops Section Overview
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month16 days, 19 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question