0

I am creating new thread everytime in the for loop .. but many times the loop takes the same filename and creates new thread.


Doesn't Work:

foreach (string s in fileEntries)
{
    t = new Thread(() => Shrink(s));
    t.Start();
}

I have tried individually for each file creating new thread hard coded providing the file name and it works fine.

Working / Needed:

Thread t = new Thread(() => Shrink(@"C:\abc.doc"));
t.Start();
Thread t1 = new Thread(() => Shrink(@"C:\123.doc"));
t1.Start();

But i have to take all the files in from a selected directory and create new thread for each in a loop.

Help would be appreciated.

Thanks

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by chintan_1671
2

Looks like you are running into the problem with capture variables. Try assigning the enumerated value to a temp variable before you use it, i.e.

foreach (string s in fileEntries) {
    string temp = s;
    t = new Thread(() => Shrink(temp));
    t.Start();
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.