foreach (string i in Enum.GetNames(typeof(Citis)))
{
string a = i;
ThreadForCity ins=new ThreadForCity(a);
Thread t=new Thread(new ThreadStart(ins.ThreadDo));
t.Start();
}
主线程中通过判断枚举 Citis从而判断需要开几个线程。
但是我现在希望在全部线程结束后输出一个信号,但是现在不知如何判断全部线程已经结束。
大大们帮忙啊
{
string a = i;
ThreadForCity ins=new ThreadForCity(a);
Thread t=new Thread(new ThreadStart(ins.ThreadDo));
t.Start();
}
主线程中通过判断枚举 Citis从而判断需要开几个线程。
但是我现在希望在全部线程结束后输出一个信号,但是现在不知如何判断全部线程已经结束。
大大们帮忙啊
Thread[]
然后判断每个线程是否isalive
Thread[] threads = new Thread[4];
int count = threads.Lenght;
for(int i = 0; i<count; ++i)
{
threads[i] = new Thread(delegate()
{
//具体操作
if(Interlocked.Decrement(ref count) == 0)
e.Set();
}).Start();
}
e.WaitOne();
Console.WriteLine("线程执行结束!");
for(int i = 0; i <count; ++i)
{
threads[i] = new Thread(delegate()
{
//具体操作
if(Interlocked.Decrement(ref count) == 0)
e.Set();
});
threads[i].Start();
}
public void testThreads()
{
ManualResetEvent[] _ManualEvents = new ManualResetEvent[10];
for (int i = 0; i < 10; i++)
{
_ManualEvents[i] = new ManualResetEvent(false);
System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback(testMethod), _ManualEvents[i]);
}
WaitHandle.WaitAll(_ManualEvents);
//All threads have completed.
}
public void testMethod(object objEvent)
{
//TODO: Add your code here
ManualResetEvent e = (ManualResetEvent)objEvent;
e.Set();
}