C#定時檢測子線程是否已經完成
class Program
{
static void Main(string[] args)
{
//主線程中啓動一個支線程,執行doSomething這樣的一個方法。
Thread thread = new Thread(new ThreadStart(ThreadRun));
thread.IsBackground = true;//這樣能隨主程序一起結束
thread.Start();
Console.ReadKey();
}
delegate void Delegate_do();
static void ThreadRun()
{
try
{
Delegate_do Delegate_do = new Delegate_do(FindAllProduct);
IAsyncResult result = Delegate_do.BeginInvoke(null, null);
while (!result.IsCompleted)
{
Console.WriteLine("子線程未完成");
Thread.Sleep(1000);//每隔1秒判斷一下是否完成
}
while (true)
{
if (result.IsCompleted)
{
Console.WriteLine("-------子線程已完成-------");
break;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
static void FindAllProduct()
{
List<int> array = new List<int>();
for (int i = 0; i < 100000000; i++)
{
array.Add(i);
}
int m = 0;
foreach (var i in array)
{
m++;
}
Console.WriteLine(m);
}
}
推薦:http://www.laikanxia.com