zoukankan      html  css  js  c++  java
  • C#定时检測子线程是否已经完毕

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


  • 相关阅读:
    MVC6 发布IIS
    Spring-Spring Bean后置处理器
    Java中重载和重写
    Java基础
    Python高级教程-多重继承
    Spring Bean声明周期
    Spring Bean作用域
    Spring-ApplicationContext容器
    Spring-BeanFactory容器
    Spring-Spring IoC容器
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/7246653.html
Copyright © 2011-2022 走看看