我在主程序中启动了两个定时器和若干个线程,在关闭程序的时候,主程序退出,但是还会有一些线程仍不退出,而驻留继续执行,而且我不知道到底启动了多少线程,怎么样才能更快更方便的清理并释放这些资源?调用线程的abort(),但是我不知道到底有多少需要关闭。

解决方案 »

  1.   

       private void EixtAll()
     {          Application.ExitThread();
              Application.Exit();           //防止非托管资源未释放造成进程无法消亡            System.Diagnostics.Process CurrentProcess = System.Diagnostics.Process.GetCurrentProcess();            if (CurrentProcess.MainWindowHandle.ToInt32() == 0)
                {
                    try
                    {
                        CurrentProcess.Kill();
                    }
                    catch
                    {
                    }
                }
            }
      

  2.   

    所有new出来的县城都放到集合里,然后遍历集合关闭
      

  3.   

    生成线程的时候设置过程的IsBackgound属性为true,就能确保主线程退出时,其余的线程都将退出
      

  4.   

    //你放在前台一定很卡,特别是监听线程
    线程.IsBackgound = true;
      

  5.   

    public bool IsBackground { set; get; }
        Member of System.Threading.ThreadSummary:
    Gets or sets a value indicating whether or not a thread is a background thread.Returns:
    true if this thread is or is to become a background thread; otherwise, false.Exceptions:
    System.Threading.ThreadStateException: The thread is dead. 
      

  6.   

    自己创建的线程需要自己管理,不能像普通对象那样置之不理。如果使用线程池,可以只管理池对象,无需过问中间使用了多少线程。如果你想更安全的让线程随主程序一起退出,可以使用BeginInvoke方法,声明一个委托,委托的对象调用BeginInvoke就开始了一个新的线程异步执行了,好处是随调用方的结束而一起结束。
      

  7.   

    try
    {
    Thread.CurrentThread.Abort();
    }
    catch (ThreadAbortException)
    {
        System.Diagnostics.Process.GetCurrentProcess().Kill();
    }
      

  8.   

    放个全局变量 比如 _close_close 默认是false
    执行
    closing事件的时候 _close等于true
    timer每次先判断_close是否是true  是的话 停止timer
      

  9.   

    所有的线程.IsBackgound = true;Application.Exit(0);
      

  10.   

    3楼方法没试过。不过1、2楼的用过。Application.Exit();下或者new的时候记录在ArraryList中,最后在逐个关下。
      

  11.   

                System.Environment.Exit(0);
                Application.Exit();
    这个应该是可以的