各位: 需求是这样的, 在Main方法里在. 我一次性要开10个线程, 十个线程跑同一个方法, 但方法传入的参数不同, 等这十个线程都跑完了, 接下来再做Main方法里其他的事情 public void Main(){for(int i = 0 ; i<10 ;i++){ thread td = new thred(方法名) // 这里开始跑线程, }// DoSomeThing() // 一定要等到线程跑完了再接下来处理下面的事.}上面这个程序只是一个比喻, 希望大家帮帮我, 当然最好有源码, 因为明天就要交差了.
AutoResetEvent waitObj2 = new AutoResetEvent(false);
void ProcessLoop1()
{
....
waitObj1.Set();
}
void ProcessLoop2()
{
....
waitObj2.Set();
}Main()
{
waitObj1.Reset();
waitObj2.Reset();
....
for(....){... new Thread...}
WaitHandler.WaitAll(waitObj1, waitObj2);
}
{
Thread t1 = new Thread(new ThreadStart(dowork));
t1.Name = "t";
t1.Start();
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i+" ");
}
Console.WriteLine();
}
static void dowork()
{
for (int j = 0; j < 10; j++)
{
Console.WriteLine("{0},{1}",Thread.CurrentThread.Name,j);
Thread.Sleep(50);
}
}
不知道是不是这样的。
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections.ObjectModel;namespace ThreadControler
{
class Program
{
static void Main(string[] args)
{
m_ResultInfoList=new Collection<string>();
Collection<Thread> threadList=new Collection<Thread>();
for (int i = 0; i < 10; i++)
{
Thread workThread = new Thread(new ParameterizedThreadStart(TestThreadProc));
threadList.Add(workThread);
workThread.Start(i);
}
m_ThreadReadyEvent.Set();
foreach (Thread tempThread in threadList)
{
tempThread.Join();
}
foreach (String tempResult in m_ResultInfoList)
{
Console.WriteLine(tempResult);
}
Thread.Sleep(5000);
}
private static Mutex m_ResultListMutex = new Mutex();
private static Collection<String> m_ResultInfoList=null;
private static EventWaitHandle m_ThreadReadyEvent = new EventWaitHandle(false, EventResetMode.ManualReset);
private static void TestThreadProc(Object index)
{
m_ThreadReadyEvent.WaitOne();
for (int i = 0; i < 5; i++)
{
Thread.Sleep(500);
}
m_ResultListMutex.WaitOne();
m_ResultInfoList.Add(index.ToString());
m_ResultListMutex.ReleaseMutex();
}
}
}