Skip to main content
 首页 » 编程设计

C#之应用程序意外终止而不留下痕迹

2024年02月13日15lvdongjie

我有一个使用 SFML.Net 的简单 C# 应用程序/游戏,目前在执行后 1 或 2 秒内停止运行/终止,没有任何警告、异常等。考虑以下代码:

public static void Main(string[] args) 
{ 
   AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException); 
 
   --> The following two lines are run 
   Console.WriteLine("Hello"); 
   Console.WriteLine("Please don't go..."); 
 
   // Run the game 
   try 
   { 
      --> This line is reached 
      Game.Run(); 
   } 
   catch (Exception e) 
   { 
      --> This line is never reached 
      Console.WriteLine(e.Message.ToString()); 
   } 
 
   --> These lines are never reached 
   Console.WriteLine("Noone ever comes here, I feel so lonely... :("); 
   Console.ReadKey(); 
   } 
} 

此时您可能认为 Game.Run() 方法有问题。奇怪的是,根据 VS 调试器,方法的第一行永远不会到达。

public static void Run() 
{ 
    try 
    { 
        --> Never reached 
        LoadContent(); 
 
        // Do stuff here 
 
        while (window.IsOpen()) 
        { 
            // The classic game loop 
        } 
    } 
    catch (Exception e) 
    { 
        --> Never reached 
        Console.WriteLine(e.Message.ToString()); 
        Console.ReadKey(); 
    } 
} 

请您参考如下方法:

我的猜测是:

或

如果您指定:

也会有帮助
  1. 你怎么知道没有到达第一行?断点?输出?
  2. 当您希望输入此功能时会发生什么?您的申请是否存在?
  3. 您是否正在逐步调试?