在控制台应用程序下,如果用户一直没输入,若干秒后就自动结束,但是用户在若干秒内输入了内容,就一直运行下去

解决方案 »

  1.   


    恩,可以做到。但是我认为线程更好~~~~~~~~没输入就Thread.Sleep();
      

  2.   

    http://topic.csdn.net/t/20061211/11/5221453.html
      

  3.   

    给你写了个小程序,自己测试一下吧
    using System;
    using System.Text;
    using System.Timers;
    class test
    {
        //已经等待的时间
        static int waittime = 0;    static void Main()
        {         Timer timer1 = new Timer();
            //每隔100毫秒监测是否到了自动退出时间
            timer1.Interval = 100;
            timer1.Elapsed += new ElapsedEventHandler(OnTimedEvent); 
            timer1.Start();        Console.WriteLine("请输入按键");
            while (true)
            {
                waittime = 0;
                ConsoleKeyInfo cki = Console.ReadKey();
                Console.WriteLine("\n------------------你输入的字符是:"+cki.KeyChar);
            }
        } 
      
        private static void OnTimedEvent(object source, ElapsedEventArgs e)   
            {
                waittime += 100;
                //DEBUG,看看计时器工作是否正常
                //Console.WriteLine("当前等待时间:" + waittime);
                //3000毫秒即3秒,若无操作3秒后自动退出
                if (waittime> 3000)
                {
                    Console.WriteLine("按键超时,自动退出");
                    //退出控制台应用程序
                    Environment.Exit(0);
                }
            }
    }
    我认为该结贴给分了,嘎嘎~~~~~~~~~