程序只能在第一次运行时会执行{do something};
好象是线程一直驻留在内存里
直到重新启动,尽管它被abort并且退出了程序。
但是如何让我不重新启动就可以每次运行它如同第一次呢。

解决方案 »

  1.   

    同意EventArgs(秦汉唐) 的观点,
    线程一旦在.NET Framework下执行,可能会在程序退出后仍占有一会的资源(也有可能是永不放弃)
    所以,在退出之前,一定要记信释放线程.不然,各式各样的系统问题会让你搞不情楚为什么你的机器会那样不稳定.
    abort并不能真真解决这个问题.
    最好是使用Dispose(),以前我遇到这个问题时用的是GC.SuperFinalize(this);
    好久不用了,呵呵.
      

  2.   

    using System;
    using System.Threading; namespace multithread
    {
    ¡¡¡¡public class Alpha
    ¡¡¡¡¡¡¡¡
    {
    ¡¡¡¡¡¡¡¡¡¡¡¡public void Beta()
    ¡¡¡¡¡¡¡¡¡¡¡¡{
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡while (true)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡{
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console.WriteLine("Alpha.Beta is running in its own thread.");
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡}
    ¡¡¡¡¡¡¡¡¡¡¡¡}
    ¡¡¡¡¡¡¡¡
    }; ¡¡¡¡¡¡¡¡public class Simple
    ¡¡¡¡¡¡¡¡{
    ¡¡¡¡¡¡¡¡¡¡¡¡public static int Main()
    ¡¡¡¡¡¡¡¡¡¡¡¡{
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console.WriteLine("Thread Start/Stop/Join Sample");¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Alpha oAlpha = new Alpha();
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡//file://ÕâÀï´´½¨Ò»¸öỊ̈߳¬Ê¹Ö®Ö´ÐÐAlphaÀàµÄBeta()·½·¨
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Thread oThread = new Thread(new ThreadStart(oAlpha.Beta));
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡oThread.Start();
               Console.WriteLine(oThread.ThreadState);
               Console.WriteLine(oThread.IsAlive);
             
            
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡while (!oThread.IsAlive)
                 Thread.Sleep(1);
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡oThread.Abort();
                 Console.WriteLine(oThread.IsAlive);
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡oThread.Join();
                 Console.WriteLine(oThread.IsAlive);
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console.WriteLine();
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console.WriteLine("Alpha.Beta has finished"); 
                 Console.WriteLine(oThread.ThreadState);
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡try 
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡{
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console.WriteLine("Try to restart the Alpha.Beta thread");
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡oThread.Start();
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡}
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡catch (ThreadStateException) 
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡{
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console.Write("ThreadStateException trying to restart Alpha.Beta. ");
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console.WriteLine("Expected since aborted threads cannot be restarted.");
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console.ReadLine();
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡}
                 
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡return 0;
           }
     }
    }
      

  3.   

    using System;
    using System.Threading; namespace multithread
    {
      public class Alpha
        
    {
          public void Beta()
          {
            while (true)
            {
              Console.WriteLine("Alpha.Beta is running in its own thread.");
            }
          }
        
    };     public class Simple
        {
          public static int Main()
          {
            Console.WriteLine("Thread Start/Stop/Join Sample");        Alpha oAlpha = new Alpha();
            //file://这里创建一个线程,使之执行Alpha类的Beta()方法
            Thread oThread = new Thread(new ThreadStart(oAlpha.Beta));
            oThread.Start();
               Console.WriteLine(oThread.ThreadState);
               Console.WriteLine(oThread.IsAlive);
             
            
            while (!oThread.IsAlive)
                 Thread.Sleep(1);
            oThread.Abort();
                 Console.WriteLine(oThread.IsAlive);
            oThread.Join();
                 Console.WriteLine(oThread.IsAlive);
            Console.WriteLine();
            Console.WriteLine("Alpha.Beta has finished"); 
                 Console.WriteLine(oThread.ThreadState);
            try 
            {
              Console.WriteLine("Try to restart the Alpha.Beta thread");
              oThread.Start();
            }
            catch (ThreadStateException) 
            {
              Console.Write("ThreadStateException trying to restart Alpha.Beta. ");
              Console.WriteLine("Expected since aborted threads cannot be restarted.");
              Console.ReadLine();
            }
                 
            return 0;
          }
     }
    }
      

  4.   

    我这里运行没问题,第二次也可以运行,
    只是最后报异常,你的线程已经Abort不能再开始
      

  5.   

    你的程序有几点问题,
    线程中一直是死循环
    而Main中
    while (!oThread.IsAlive)
          Thread.Sleep(1);//最多走到这儿
    oThread.Abort();//并不执行