2012-02-02 95 views
1

我有一個循環ping請求,當我點擊按Ctrl +Ç,它打破循環,給我像正常CMD靜,我想的方法,但是當我點擊按Ctrl +ç,我得到禁用控制+ C關閉程序

按任意鍵繼續...... < <

,然後程序關閉。

例如:ping -t google.com < <將ping操作無限循環谷歌,但我需要打破循環,只有當我點擊按Ctrl +ç

private void _t(string website) 
{ 
    Ping pingSender = new Ping(); 
    string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; 
    byte[] buffer = Encoding.ASCII.GetBytes(data); 
    int timeout = 10000; 
    try 
    { 
     PingOptions options = new PingOptions(64, false); 
     PingReply send0 = pingSender.Send(website, timeout, buffer, options); 
     Console.WriteLine("\nPinging {0} [{1}] With {2} bytes of data :", website, send0.Address.ToString(), send0.Buffer.Length); 
     while (1 < 2) 
     { 
      PingReply reply = pingSender.Send(website, timeout, buffer, options); 

      if (reply.Status == IPStatus.Success) 
      { 
       Console.WriteLine("Reply from {0}: Bytes {1} time={2} TTL={3}", reply.Address.ToString(), reply.Buffer.Length, reply.RoundtripTime/5, reply.Options.Ttl); 
      } 
      else 
      { 
       Console.WriteLine("Request timed out."); 
      } 
     } 
    } 
    catch 
    { 
     Console.WriteLine("Ping request could not find host {0} ", website + ".Please check the name and try again."); 
    } 
} 

但是相反使用while (1<2)的,我想用這樣的:

while (ConsoleKeyInfo.Equals("Control + c") not clicked) // sure it is wrong , but that`s what I wanna achieve 
{ 
    PingReply reply = pingSender.Send(website, timeout, buffer, options); 

    if (reply.Status == IPStatus.Success) 
    { 
     Console.WriteLine("Reply from {0}: Bytes {1} time={2} TTL={3}",  reply.Address.ToString(), reply.Buffer.Length, reply.RoundtripTime/5, reply.Options.Ttl); 
    } 
    else 
    { 
     Console.WriteLine("Request timed out."); 
    } 
} 
+0

您的意思是統計數據不是靜態數據嗎? – ono2012 2017-07-25 23:18:47

回答

3

有一個相當完整的教程(附代碼)here。我做了一個快速測試,他們的例子確實有效(想象一下)。

這裏是他們的代碼:

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Xml; 
using System.Text; 
using System.Text.RegularExpressions; 
using System.Runtime.InteropServices; 
using System.Threading; 
using System.Reflection; 

namespace ConsoleApplication1 
{ 

    class ControlChecker 
    { 
     #region GLOBAL VARS 
     private static readonly Mutex mutex = new Mutex(true, Assembly.GetExecutingAssembly().GetName().CodeBase); 
     private static bool _userRequestExit = false; 
     private static bool _doIStop = false; 
     static HandlerRoutine consoleHandler; 
     #endregion 

     [DllImport("Kernel32")] 
     public static extern bool SetConsoleCtrlHandler(HandlerRoutine Handler, bool Add); 

     // A delegate type to be used as the handler routine for SetConsoleCtrlHandler. 
     public delegate bool HandlerRoutine(CtrlTypes CtrlType); 

     // An enumerated type for the control messages sent to the handler routine. 
     public enum CtrlTypes 
     { 
      CTRL_C_EVENT = 0, 
      CTRL_BREAK_EVENT, 
      CTRL_CLOSE_EVENT, 
      CTRL_LOGOFF_EVENT = 5, 
      CTRL_SHUTDOWN_EVENT 
     } 

     /// <summary> 
     /// 
     /// </summary> 
     /// <param name="ctrlType"></param> 
     /// <returns></returns> 
     private static bool ConsoleCtrlCheck(CtrlTypes ctrlType) 
     { 
      // Put your own handler here 
      switch (ctrlType) 
      { 
       case CtrlTypes.CTRL_C_EVENT: 
        _userRequestExit = true; 
        Console.WriteLine("CTRL+C received, shutting down"); 
        break; 

       case CtrlTypes.CTRL_BREAK_EVENT: 
        _userRequestExit = true; 
        Console.WriteLine("CTRL+BREAK received, shutting down"); 
        break; 

       case CtrlTypes.CTRL_CLOSE_EVENT: 
        _userRequestExit = true; 
        Console.WriteLine("Program being closed, shutting down"); 
        break; 

       case CtrlTypes.CTRL_LOGOFF_EVENT: 
       case CtrlTypes.CTRL_SHUTDOWN_EVENT: 
        _userRequestExit = true; 
        Console.WriteLine("User is logging off!, shutting down"); 
        break; 
      } 

      return true; 
     } 

     /// <summary> 
     /// Main entry point 
     /// </summary> 
     /// <param name="args"></param> 
     /// <returns></returns>   
     static int Main(string[] args) 
     { 
      try 
      { 
       //make sure we only have one.... 
       if (!mutex.WaitOne(TimeSpan.Zero, true)) 
       { 
        Console.WriteLine("Another instance already running"); 
        Thread.Sleep(5000); 
        return 1; 
       } 

       //save a reference so it does not get GC'd 
       consoleHandler = new HandlerRoutine(ConsoleCtrlCheck); 

       //set our handler here that will trap exit 
       SetConsoleCtrlHandler(consoleHandler, true); 

       DoMyTask(); 

       return 0; 
      } 
      catch (Exception x) 
      { 
       Console.WriteLine("Main Error [{0}]", x.Message); 
       return -1; 
      } 
     } 



     /// <summary> 
     /// Run the export 
     /// </summary> 
     /// <param name="pAuthority"></param> 
     /// <returns></returns> 
     private static void DoMyTask() 
     { 
      //execcute until we have no more records to process 
      while (!_doIStop) 
      { 
       //did user request exit? 
       if (_userRequestExit) 
       { 
        _doIStop = true; //set flag to exit loop. Other conditions could cause this too, which is why we use a seperate variable 
        Console.WriteLine("Shutting down, user requested exit"); 
        break; 
       } 

       //do some other stuff here 
       Console.WriteLine(String.Format("{0}, no exit requested yet...", DateTime.Now)); 
       //sleep 1 second 
       Thread.Sleep(1000);  
      } 
     } 
    } 
} 
+0

真的很感謝...因爲它的工作 – 2012-02-02 01:27:28

+0

我的評論更多的是當我遇到問題時,我傾向於找到非工作示例,所以我很驚訝這個實際工作。 – 2012-02-02 01:28:23

6

我可能有點晚了,但這裏有一個更好的辦法,一個所有平臺上的工作原理:

Console.TreatControlCAsInput = true; 
+1

所有平臺? TreatControlCAsInput不僅僅是Windows的東西嗎?例如,我沒有在Linux環境中看到它。 – Dronz 2012-10-12 04:15:06

+1

@Dronz在Linux下,Mono可以正常工作。 – dabide 2013-08-26 14:29:40

+0

@dabide謝謝! – Dronz 2014-03-25 20:59:42

0

另一種方式是處理控制檯.CancelKeyPress事件:

Console.CancelKeyPress += Console_CancelKeyPress 

這允許您保留快捷方式,但決定是否退出一個案件以案例爲基礎

private static void HandleConsole_CancelKeyPress(ConsoleCancelEventArgs consoleCancelEventArgs) 
{ 
    if (doNotExitYet) { 
     consoleCancelEventArgs.Cancel = true; 
    } 
}