2013-04-21 113 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()代替。