我编了一个ping软件,现在只能一直循环ping下去,如何实现一直检测输入,当我输入Ctrl+c或者别的什么时停止循环,显示结果。

解决方案 »

  1.   

    这样看你的程序是什么类型的,如果是WinForm的,可以处理窗体的消息,如果控制能项目可以读取Console.ReadLine的值。
      

  2.   

    应该是个多线程的程序,程序运行后另起一个线程循环ping,当主线程中输入Ctrl+c时结束循环ping线程,
    也是新手,不知说的对不对,
      

  3.   

    在你的循环里设置一个信号量,通过控制信号量来控制是否开始停止,给你个实例代码。
        public sealed class MultiThreadWorker
        {
            bool _threadSwitch = false;
            bool _pauseSwitch = false;
            ManualResetEvent _resetEvent = new ManualResetEvent(false);
            Thread[] _workers = null;
            readonly object _locker = new object();
            int _intervalMillisconds = 0;         public delegate bool ThreadWorkingEentHander(object sender, EventArgs e);        public event ThreadWorkingEentHander OnThreadWorking;
            readonly EventArgs args = new EventArgs();        private bool RaiseThreadWorking()
            {
                if (OnThreadWorking != null)
                {
                    return OnThreadWorking(this, args);
                }
                return false;
            }        public MultiThreadWorker(Func<object,EventArgs,bool> eventHandler)
            {
                this.OnThreadWorking += new ThreadWorkingEentHander(eventHandler);
            }
            public void Start(int threadCount,int intervalMillisconds)
            {
                _intervalMillisconds = intervalMillisconds;
                lock(_locker)
                {
                    if (_workers == null)
                    {
                        _threadSwitch = true;
                        _workers = new Thread[threadCount];
                        for(int i = 0;i < _workers.Length; i ++)
                        {
                        _workers[i] = new Thread(Run);
                        _workers[i].IsBackground = true;
                        _workers[i].Name = i.ToString();
                        _workers[i].Start();
                        }
                    }
                }        }        void Run()
            {
                while (_threadSwitch)
                {
                    int timestamp = 0;
                    try
                    {
                        timestamp = System.Environment.TickCount;
                        if (!RaiseThreadWorking())
                        {
                            break;
                        }
                        if (_pauseSwitch)
                        {
                            _resetEvent.WaitOne();
                        }
                    }
                    catch (System.Exception ex)
                    {
                        LogManager.WriteError(ex);
                    }
                    int elapsedTickCount = Environment.TickCount - timestamp;
                    int sleepInterval = _intervalMillisconds - elapsedTickCount;
                    sleepInterval = sleepInterval > 0 ? sleepInterval : 0;
                    Thread.Sleep(sleepInterval);
                }
            }        public void Continue()
            {
                lock (_locker)
                {
                    if (!_pauseSwitch)
                    {
                        return;
                    }
                    _pauseSwitch = false;
                    _resetEvent.Set();
                }
            }        public void Pause()
            {
                lock (_locker)
                {
                    if (_pauseSwitch)
                    {
                        return;
                    }
                    _resetEvent.Reset();
                    _pauseSwitch = true;
                }
            }        public void Stop()
            {
                lock (_locker)
                {
                    if (_workers == null)
                    {
                        return;
                    }
                    _threadSwitch = false;
                    _pauseSwitch = false;
                    _resetEvent.Set();
                    for (int i = 0; i < _workers.Length; i++)
                    {
                        if (_workers[i].IsAlive)
                        {
                            try
                            {
                                _workers[i].Abort();
                            }
                            catch
                            {
                            }
                        }
                    }
                    _workers = null;
                }
            }        public static void RuntimeWatch(Action action,ref int totalTime)
            {
    #if DEBUG
                int timeStamp = Environment.TickCount;
    #endif
                action();
    #if DEBUG
                timeStamp = Environment.TickCount - timeStamp;
                LogManager.WriteDebug("Runtime:" + timeStamp.ToString());
                Interlocked.Add(ref totalTime, timeStamp);
    #endif
            }
        }你要终止调用Pause方法就可以了但你要知道怎样界面能接受Ctrl的命令
    是console还是winform?
      

  4.   

    那就用我这个类就可以了,把这个类作为一个静态变量用Console.ReadKey()来读取你输入的字符
    如果是ctl,就调用静态变量的Pause()方法就行了,如果继续就调用Continue就ok。
      

  5.   

    using System;
    using System.Collections.Generic;
    using System.Text;namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                while (true)
                {
                    if (Console.KeyAvailable)
                    {
                        ConsoleKeyInfo input = System.Console.ReadKey(false);
                        if (input.Modifiers == ConsoleModifiers.Control && input.Key == ConsoleKey.C)
                            break;
                    }
                    Console.WriteLine("test");
                    System.Threading.Thread.Sleep(1000);
                }
            }
        }
    }
      

  6.   

    我觉得
    定义一个全局变量(bool isgo = true),如果isgo==true就循环,
    在按ctrl+C时触发事件把isgo赋值成false ,循环退出。只是怎么想的,没去写代码。只是给点建议。
      

  7.   

    class Program
        {
            static MultiThreadWorker _worker = new MultiThreadWorker(Test);
            static void Main(string[] args)
            {
                _worker.Start(1,1);
                while (true)
                {
                        ConsoleKeyInfo input = System.Console.ReadKey(false);
                    if (Console.KeyAvailable)
                    {                    if (input.Modifiers == ConsoleModifiers.Control && input.Key == ConsoleKey.C)
    {
    _worker.Stop();
                            break;
    }
                    }
                    if(input.Char.Equals('p'))
    {
                    _worker.Pause();
    }
    if(input.Char.Equals('g'))
    {
    _work.Continue();
    }    
                }
            }
     
    static bool Test(object obj,EventArgs e)
    {
    //send ping cmd
    }
        }