c# Multi-threaded for loop

Posted on 2009-04-23
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
    LVL 15

    Accepted Solution

    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

    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

    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

    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

    Excellent feedback, thank you again.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now