码迷,mamicode.com
首页 > 编程语言 > 详细

C#定时检测子线程是否已经完成

时间:2015-08-11 23:22:48      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:c#   delegate_do   多线程   检测   

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


版权声明:本文为博主原创文章,未经博主允许不得转载。

C#定时检测子线程是否已经完成

标签:c#   delegate_do   多线程   检测   

原文地址:http://blog.csdn.net/wantianwen/article/details/47429141

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!