【发布时间】:2012-09-16 18:49:27
【问题描述】:
我试图让(我的测试版)应用程序尽可能地运行,所以我在Program.cs 中放置了另一个try-catch,以防出现一些严重错误并意外关闭应用程序。并且在catch 中,我重写了Application.Run() 方法,以便应用程序可以在因任何原因终止后自行恢复。
针对这种特定场景制定这样的计划是否正确?
如果不正确,那么还有什么建议可以让程序继续运行?
这是演示我的意思的示例代码:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Hossein;
using Pishro.Classes;
namespace Pishro
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMain());
}
catch(Exception exc)
{
API.SaveAndShowLog(exc);
Application.Run(new frmMain());
}
}
}
}
【问题讨论】:
-
如果您需要保持运行 - 我向您提出这个问题.. 如果再次发生异常怎么办?
-
如果应用程序因错误而退出,IMO 您应该记录异常并向描述错误的用户显示
MessageBox。 -
在您执行此操作之前,请阅读此内容。 stackoverflow.com/questions/10061287/…
-
@BugFinder:这就是我首先问这个问题的原因。
-
@MatthewRz:使用 SaveAndShowLog() 方法已经完成。同时所有发生的错误日志都显示在管理部分,我只需要一种方法在程序继续进行时尽可能多地进行测试工作。