在timer1.Change(Timeout.Infinite,0);
后面加上 break;

解决方案 »

  1.   

    我一加break就跳出循环了,没办法阻塞控制台了,不行的
      

  2.   

    using System;
    using System.Threading;namespace ConsoleApplication1
    {
    /// <summary>
    /// Class1 的摘要说明。
    /// </summary>
    class Class1
    {
    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    //private static 

    [STAThread]
    static void Main(string[] args)

       State s = new State();
       Timer  timer1 = new Timer(new TimerCallback(Queue_RecvMessageThread),s,0,1000);
       s.timer = timer1;
       
       string cmd = "";
       
    do
    {
    cmd = Console.ReadLine().ToLower();
    switch(cmd)
    {
    case "stop":
    s.command = "STOP";
    break;

    default:
    Console.Write("? For Help>>");
    break;
    }
    }while(cmd != "exit");
    }

    static void Queue_RecvMessageThread(object o)
    {
    State s = o as State;
    switch(s.command)
    {
    case "STOP":
    Console.WriteLine("STOP!!!");
    s.timer.Change(Timeout.Infinite,0);
    return; default:
    break;
    }

    Console.WriteLine("working .. ...");
    }
    }
    class State
    {
    internal string command;
    internal Timer timer;
    }
    }
      

  3.   

    这回可以了吧.关键在于,Timer的回调函数在系统线程池中执行,是另外一个线程,这一点同Form中的计时器不一样..
      

  4.   

    同意 haiwangstar(八月桂花香) ,自己控制。