简体   繁体   English

使用不同的变量同时运行多个相同的线程

[英]Running multiple of same thread at same time with different variables

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 方法1

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) (C#,WinForms)

You simply just have to add a sleep 您只需要添加睡眠

Thread.Sleep(1000); 

that should do the trick :D 那应该可以解决问题:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM