2017-07-24 119 views
0

我需要同时多次运行相同的线程/方法。使用不同变量同时运行多个相同线程

我有什么这个(实际名称变更)

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

方法一

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 
    } 
} 

它运行多个线程,让我们说在这个例子中三个线程。因此三个消息框以相同的数字弹出所有消息框,每次都需要它们是不同的数字。

感谢您的帮助!

(C#的WinForms)

+0

你可以添加方法1的情况?如果Method1不包含一些使线程保持活动状态的循环,则线程将在方法结束时终止。 – Mick

+0

我想你需要将'number'作为参数传递给'Method1',否则你的所有线程工作都是一样的。 –

+0

你如何确定哪些线程正在运行? – Mick

回答

0

您只需只需要添加一个睡眠

Thread.Sleep(1000); 

应该做的伎俩:d

相关问题