2012-10-17 49 views
0

Im使用MSDN網站上的自定義編譯器。當我嘗試編譯/運行一些測試代碼(拖放到.exe)時,控制檯窗口打開,然後關閉,而不是保持打開狀態,直到我選擇關閉它。我如何保持開放?自定義編譯器閃爍打開然後關閉

來源:http://msdn.microsoft.com/en-us/magazine/cc136756.aspx#S8

Program.cs的

if (args.Length != 1) 
     { 
      // Display title, reset cursor to normal, add space 
      Console.WriteLine("Alt ver 1.0 (Alpha)"); 
      Console.WriteLine(); 
      Console.ReadLine(); 
      try 
      { 
       Scanner scanner = null; 
       using (TextReader input = File.OpenText(args[0])) 
       { 
        scanner = new Scanner(input); 
       } 
       Parser parser = new Parser(scanner.Tokens); 
       CodeGen codeGen = new CodeGen(parser.Result, Path.GetFileNameWithoutExtension(args[0]) + ".exe"); 
      } 
      catch (Exception e) 
      { 
       Console.Error.WriteLine(e.Message); 
       Console.ReadLine(); 
      } 
     } //if 

回答

2

在最後添加Console.ReadLine();try塊內 試試這個

if (args.Length != 1) 
      { 
       // Display title, reset cursor to normal, add space 
       Console.WriteLine("Alt ver 1.0 (Alpha)"); 
       Console.WriteLine(); 
       Console.ReadLine(); 
       try 
       { 
        Scanner scanner = null; 
        using (TextReader input = File.OpenText(args[0])) 
        { 
         scanner = new Scanner(input); 
        } 
        Parser parser = new Parser(scanner.Tokens); 
        CodeGen codeGen = new CodeGen(parser.Result, Path.GetFileNameWithoutExtension(args[0]) + ".exe"); 
       } 
       catch (Exception e) 
       { 
        Console.Error.WriteLine(e.Message); 
        Console.ReadLine(); 
       } 
finally 
{ 
Console.Readkey(); 
} 
      } //if 
else 
{ 
Console.WriteLine("no args"); 
Console.ReadKey(); 
} 

編輯:---傳球參數問題 我做了這個程序,它儘可能完美的作品如獲得的文件名作爲參數
請看看

class Program 
    { 
     static void Main(string[] args) 
     { 
      if (args.Length > 0) 
      { 
       foreach (var arg in args) 
       { 
        Console.WriteLine(arg); 
       } 
       Console.ReadKey(); 
      } 
      else 
      { 
       Console.WriteLine("NO ARGS"); 
       var fileName = Console.ReadLine(); 
       Main(new string[] { fileName }); 
      } 
     } 
    } 
+0

試了一下,但還是同樣的結果 – SpicyWeenie

+0

我已經編輯我的答...確保該參數表是正確傳遞 –

+0

確定控制檯現在保持運行狀態,但不是編譯我的代碼,而是顯示「無參數」。 – SpicyWeenie