C# 用IsAlive来确定线程结束时间
2010-02-28 19:16
239 查看
using System;
using System.Threading;
// Use IsAlive to wait for threads to end.(用IsAlive来确定线程结束时间).
class MyThread
{
public int count;
public Thread thrd;
public MyThread(string name)
{
count = 0;
thrd = new Thread(this.run); // use method group conversion
thrd.Name = name; // set the name of the thread
thrd.Start(); // start the thread(开启线程)
}
// Entry point of thread.
void run()
{
Console.WriteLine(thrd.Name + " starting.");
do
{
Thread.Sleep(500);
Console.WriteLine("In " + thrd.Name + ", count is " + count);
count++;
} while (count < 10);
Console.WriteLine(thrd.Name + "terminating.");
}
}
class MoreThreads
{
public static void Main()
{
Console.WriteLine("Main thread starting.");
//Construct three threads.(建立三个线程)
MyThread mt1 = new MyThread("child #1");
MyThread mt2 = new MyThread("child #2");
MyThread mt3 = new MyThread("child #3");
do
{
Console.Write(".");
Thread.Sleep(100);
} while (mt1.thrd.IsAlive &&
mt2.thrd.IsAlive &&
mt3.thrd.IsAlive);
Console.WriteLine("Main thread ending.");
}
}
using System.Threading;
// Use IsAlive to wait for threads to end.(用IsAlive来确定线程结束时间).
class MyThread
{
public int count;
public Thread thrd;
public MyThread(string name)
{
count = 0;
thrd = new Thread(this.run); // use method group conversion
thrd.Name = name; // set the name of the thread
thrd.Start(); // start the thread(开启线程)
}
// Entry point of thread.
void run()
{
Console.WriteLine(thrd.Name + " starting.");
do
{
Thread.Sleep(500);
Console.WriteLine("In " + thrd.Name + ", count is " + count);
count++;
} while (count < 10);
Console.WriteLine(thrd.Name + "terminating.");
}
}
class MoreThreads
{
public static void Main()
{
Console.WriteLine("Main thread starting.");
//Construct three threads.(建立三个线程)
MyThread mt1 = new MyThread("child #1");
MyThread mt2 = new MyThread("child #2");
MyThread mt3 = new MyThread("child #3");
do
{
Console.Write(".");
Thread.Sleep(100);
} while (mt1.thrd.IsAlive &&
mt2.thrd.IsAlive &&
mt3.thrd.IsAlive);
Console.WriteLine("Main thread ending.");
}
}
相关文章推荐
- 线程start后,确定已经不再isAlive了,但是再次start会报错。
- C# 多线程join的用法,等待多个子线程结束后再执行主线程
- C#线程 在某一时间内,只有N个线程在并发执行,其余都在队列中的实现
- C# 中如何判断线程是否结束
- C#线程 在某一时间内,只有N个线程在并发执行,其余都在队列中的实现
- c# winform 通过线程和委托制作时间控件
- C#线程 在某一时间内,只有N个线程在并发执行,其余都在队列中的实现(转载)
- C#根据当前时间确定日期范围(本周、本月、本季度、本年度)[转]
- c# 获取 本周、本月、本季度、本年 的开始时间或结束时间
- C# 根据第几周和季度 获取开始时间和结束时间
- Unity之C#——异步委托开启线程,三种方法检测结束
- C#根据当前时间确定日期范围(本周、本月、本季度、本年度及常见日期方法荟萃 )
- C#根据当前时间确定日期范围(本周、本月、本季度、本年度及常见日期方法荟萃
- .Net线程足迹(三)——IsAlive属性
- C#UI线程产生的子线程如何在UI结束时,干净的结束
- C#计算时间--按照日期确定第几周 按照第几周确定日期范围
- asp.net C#计算时间--按照日期确定第几周 按照第几周确定日期范围
- C#计算时间--按照日期确定第几周 按照第几周确定日期范围
- C# 程序退出前 强行结束线程
- C#线程 在某一时间内,只有N个线程在并发执行,其余都在队列中的实现