0

I need to run the same thread/method multiple times at the same time.

What i have it this (Actual Names changed)

        foreach (string number in liststrings)
        {
            ThreadStart work = delegate { Method1(number); };
            Thread thr = new Thread(work);
            thr.IsBackground = true;
            thr.Start();
        }

Method1

public static Method1(string numb)
{            
    Random rnd = new Random();
    int threadC = rnd.Next(1,100000);
    MessageBox.Show(threadC.ToString());
    int i = 1;
    while(i==1)
    {
        //Do Stuff
    }
}

It runs multiple threads, let's say three threads for this example. So three messageboxes popup all of them with the same numbers, need it to be different numbers each time.

Thanks for the help!

(c#, winforms)

Coderz
  • 215
  • 2
  • 7
  • 20
  • Can you add the implementation of Method1 ? If Method1 doesn't contain some loop to keep the thread alive the thread will terminate at the end of the method. – Mick Jul 24 '17 at 01:29
  • I think you need to pass `number` as an argument to `Method1` otherwise all your thread works are identical. – Cheng Chen Jul 24 '17 at 01:30
  • How are you determining what threads are running? – Mick Jul 24 '17 at 01:33
  • the real method1 has number passed as an argument and a while loop in it forgot to add thanks. The problem is that in the end it only run Method1 once even tho the list has multiple items in it – Coderz Jul 24 '17 at 01:33
  • When I run your code I can definitely see multiple threads being generated. I'd suggest you're confused... try using the Visual Studio debug location toolbar – Mick Jul 24 '17 at 01:42
  • Alternative try adding to method1 Console.WriteLine(Thread.CurrentThread.ManagedThreadId); you should see a different thread id for each item in liststrings – Mick Jul 24 '17 at 01:45
  • 4
    You could be observing an issue with captured loop variable (Google it). If you're using an older version of the compiler and your loop runs fast, I expect all your threads to only see the *last* value in your `liststrings` collection. Try changing the first line inside your loop to `string n = number; ThreadStart work = delegate { Method1(n); };` and see if you get expected behaviour. – Kirill Shlenskiy Jul 24 '17 at 02:00
  • @KirillShlenskiy I don't think that applies in this case. That trap is associated with lambdas, which get a reference to the value passed, the outer loop changes the reference causing every lambda to get the value for the last item in the iteration. Creating a temporary variable is used to create a seperate reference for each lambda. In this case it's not a lambda and in my testing each thread is getting a different element of the array – Mick Jul 24 '17 at 03:12
  • @KirillShlenskiy you're talking about the https://stackoverflow.com/questions/3416758/outer-variable-trap right ? – Mick Jul 24 '17 at 03:13
  • @Mick, correct, that's exactly what I'm talking about. Delegates and lambdas have identical variable capture behaviour and suffer from the same problem on older systems. The reason you're not observing this behaviour is likely because you're using C# 5 or above, which automatically copies loop iteration variables to a local (see first paragraph of https://blogs.msdn.microsoft.com/ericlippert/2009/11/12/closing-over-the-loop-variable-considered-harmful/) – Kirill Shlenskiy Jul 24 '17 at 03:23
  • @KirillShlenskiy thanks for the info... didn't know they had changed that – Mick Jul 24 '17 at 04:36
  • Hey why not try parallel foreach instead of the for loop and drop the threading inside altogether? https://stackoverflow.com/questions/12251874/when-to-use-a-parallel-foreach-loop-instead-of-a-regular-foreach – screig Jul 24 '17 at 11:38
  • 1
    They run concurrently for sure. You must have multiple cores to make it parallel. Please include to the while cycle: Console.WriteLine(Thread.CurrentThread.ManagedThreadId); You will see, that they are running, BUT DO NOT GET CONFUSED BY VISUAL STUDIO, it shows one change at the time and it likes to switch between threads, but as you have only one method, it can get confusing and you may draw wrong conclusion about running threads one at the time... – ipavlu Jul 24 '17 at 20:12
  • @ipavlu You are right, it is running the thread multiple times, but it is also having the same variables in each thread. Why is that? I did a thing like this ` Random rnd = new Random();threadC = rnd.Next(1,100000);MessageBox.Show(threadC.ToString());` and each thread show the same number instead of a random on each. – Coderz Jul 26 '17 at 22:44
  • @Coderz If you want different randomness, then you have to provide Random constructor with seed parameter and different seed for each thread. Then you are going to get different random sequences. – ipavlu Jul 27 '17 at 14:15

1 Answers1

1

You simply just have to add a sleep

Thread.Sleep(1000); 

that should do the trick :D

Coderz
  • 215
  • 2
  • 7
  • 20