2013-04-21 37 views
2

我正在Linux mint 14上建立一個MONO(C#)應用程序,但我有一些問題。我已經在選項中啓用了外部控制檯,並且在調試它的工作時相當好,但是在部署後(在debug文件夾中打開.exe),應用程序會在Console.ReadLine()後立即退出;任何想法傢伙?C#單聲道Console.ReadLine退出

public static void InitializeUI() 
     { 
      Console.WriteLine("On the bottom line enter the command \"START\" followed by a space and a number representing on how many hours to check for new posts. You can always stop the execution with Ctrl+C \r\n \r\nExample : START 6"); 
      ParseCMD(); 
      Console.ReadLine(); 
     } 
     private static void ParseCMD() 
     { 
      string cmd = Console.ReadLine(); 
      string[] commands = cmd.Trim().Split(new string[] {" "},StringSplitOptions.RemoveEmptyEntries); 
      int delay = 0; 
      if (commands[0].ToLower() == "start" && int.TryParse(commands[1],out delay)) 
      { 
       MainLogic.StartLogic(delay); 
      } 
      else 
      { 
       Console.WriteLine("Wrong command"); 
       ParseCMD(); 
      } 
     } 

退出

string cmd = Console.ReadLine(); 
+1

請問您是否可以添加一些您正在做的事情的代碼示例? – MajesticRa 2013-04-21 07:30:22

+1

用try catch塊包裝你的方法,並打印異常,並再次把控制檯讀取線。這可能是由於例外,您需要在繼續前找到確切的異常。 – Damith 2013-04-21 07:40:27

+0

還要注意,如果用戶只是在不輸入任何數據的情況下按下'',命令[]'數組將爲空,命令[0] .ToLower()會拋出一個'IndexOutOfRangeException'。 – 2013-04-21 07:59:38

回答

0
public static void Main(string[] args) 
    { 
     Console.WriteLine("On the bottom line enter the command \"START\" followed by a space and a number representing on how many hours to check for new posts. You can always stop the execution with Ctrl+C \r\n \r\nExample : START 6"); 


     if(!ParseCMD(readKeey())) 
     { 
      ParseCMD(readKeey()); 
     } 

    }private static string readKeey() 
    { 
     ConsoleKeyInfo cki; 
     Console.TreatControlCAsInput = true; 

     string temp=""; 
     do 
     { 
      cki = Console.ReadKey(); 
      temp=temp+cki.KeyChar; 

     } while (cki.Key != ConsoleKey.Enter); 
     return temp; 
    } 

    private static bool ParseCMD(string text) 
    { 
     try{ 
      string cmd = text; 
      string[] commands = cmd.Trim().Split(new string[] {" "},StringSplitOptions.RemoveEmptyEntries); 
      int delay = 0; 
      if (commands[0].ToLower() == "start" && int.TryParse(commands[1],out delay)) 
      { 
       Console.WriteLine("Right command you enter:" + commands[0] + commands[1]); 
       return true; 
      } 
      else 
      { 
       Console.WriteLine("Wrong command"); 

       return false; 
      } 
     } 
     catch(Exception ex) 
     { 
      Console.WriteLine("ex.Message:"+ex.Message); 
      return false; 
     } 
    } 
}} 

後,我想這是任何關於單聲道,你可以請嘗試Console.ReadKey()代替。