0
 for (long key = 0; key < 5; key++)
{
var processingThread = new Thread(() => Setup(key));
processingThread.Start();
}

I want to execute the Setup(key) function with each key value but at the same time on multiple windows..

Vimal CK
  • 3,435
  • 1
  • 22
  • 45
Nandu PH
  • 125
  • 1
  • 2
  • 9
  • 4
    What happens? You've been bitten by the [closing over the loop variable](http://blogs.msdn.com/b/ericlippert/archive/2009/11/12/closing-over-the-loop-variable-considered-harmful.aspx)? – Sriram Sakthivel May 04 '15 at 08:37
  • take a look at [ParameterizedThreadStart](https://msdn.microsoft.com/en-us/library/system.threading.parameterizedthreadstart%28v=vs.110%29.aspx) – Yoav May 04 '15 at 08:37
  • 2
    Being not %100 sure I understand what you are trying to achieve, I think Parallel.For can be used in this case.See [here](https://msdn.microsoft.com/en-us/library/dd460713(v=vs.110).aspx), and [here](http://stackoverflow.com/questions/12405938/save-time-with-parallel-for-loop). – Oğuz Sezer May 04 '15 at 08:39
  • thanks Yoav.. But in my case i want to execute the setup() function with key=1 in one window, then setup() function with key=2 in second window, and so on.. – Nandu PH May 04 '15 at 08:42
  • for (long key = 0; key < 5; key++) { var processingThread = new Thread(() => Setup(key)); processingThread.Start(); } setup(key) {//do some processing} – Nandu PH May 04 '15 at 08:45
  • is there any other way to execute setup(1); setup(2); setup(3); setup(4); setup(5); in 5 different forms concurrently?? – Nandu PH May 04 '15 at 09:00

4 Answers4

0

You need to capture a local copy of key within the for loop otherwise by the time the threads actually call Setup the value of key has become 5. If you capture a local copy then that value doesn't change and all works as expected.

for (long key = 0; key < 5; key++)
{
    var localKey = key;
    var processingThread = new Thread(() => Setup(localKey));
    processingThread.Start();
}
Enigmativity
  • 97,521
  • 11
  • 78
  • 153
-1

Check out the Parallel.ForEach() and Parallel.For() methods.

https://msdn.microsoft.com/en-us/library/dd460720(v=vs.110).aspx

Explicitly creating a new thread has large overhead and should be avoided. Only do it if have good reason and already have considered using a thread pool based solution (such as PTL, or similar).

Mahol25
  • 3,001
  • 2
  • 19
  • 19
-1
for (long key = 0; key < 5; key++)
{
    var processingThread = new Thread(Setup);
    processingThread.Start(key);
}

Setup parameter type must be changed to object (and casted, if needed)

MatteoSp
  • 2,842
  • 5
  • 24
  • 34
  • Setup is actually a method, which is accepting the loop variable as argument. – Nandu PH May 04 '15 at 08:58
  • Of course. It's signature probably is "void Setup(long key)", isn't it? It need to be changed in "void Setup(object key)". – MatteoSp May 04 '15 at 09:01
  • how can i start the thread once, make it loop, and do Thread.sleep(500) with each iteration? – Nandu PH May 04 '15 at 09:47
  • 1
    I think you'd better ask another question (and mark this one as answered if your original problem was solved) – MatteoSp May 04 '15 at 09:56
-2

If Parallel.For() does not provide the trick you can pass them all a AutoResetEvent.
Call Wait() in all your delegates and then call Set() after creating all the threads.
Please take notice in the fact that the system does

// THIS ISN'T TESTED AND IS WRITTEN HERE, SO MIND THE SYNTAX, THIS MIGHT NOT COMPILE !!

 AutoResetEvent handle = new AutoResetEvent(true);

 for (long key = 0; key < 5; key++)
 {
    var processingThread = new Thread(() => 
        {
           handle.Wait(); 
           Setup(key)
        } );
    processingThread.Start();
 }

 handle.Set();
users at 4325010
  • 3,596
  • 4
  • 28
  • 46
eran otzap
  • 11,360
  • 20
  • 73
  • 127
  • I Wrote this on my smart phone and not in Visual Studio. I mentioned this in the answer. the general idea works. I gave it as an alternative to Parallel.For If some one marks down. please stat he reason.. – eran otzap Feb 08 '16 at 12:05