有沒有一種方法(除了WerAddExcludedApplication在Windows XP中無法使用)禁用窗口「Application has encountered a problem and need to close」從出現當我的應用程序崩潰?禁用「Foo遇到問題並需要關閉」窗口
alt text http://i37.tinypic.com/2vvw6yd.png
(距離Bil Simser」 blog拍攝的圖像)
我需要這在Windows XP中運行。
有沒有一種方法(除了WerAddExcludedApplication在Windows XP中無法使用)禁用窗口「Application has encountered a problem and need to close」從出現當我的應用程序崩潰?禁用「Foo遇到問題並需要關閉」窗口
alt text http://i37.tinypic.com/2vvw6yd.png
(距離Bil Simser」 blog拍攝的圖像)
我需要這在Windows XP中運行。
沒有未處理的異常。
你不能處理所有的例外......至少它不總是可能的 – 2009-11-05 13:52:30
是的,你可以。處理所有你能想到的異常,然後爲未處理的異常添加一個處理程序來捕捉你沒有處理的異常。這在99.9%的時間內起作用;對於另外的0.01%,修復你的錯誤代碼。 – 2009-11-05 17:42:10
不是一個非常有用的答案! – 2009-11-05 19:17:42
您需要處理Application.ThreadException
和AppDomain.CurrentDomain.UnhandledException
事件。
這些都是事件,所以你可以對他們採取行動,而不是真正做很多處理,但那不僅僅是重點。您可能沒有其他選擇,一旦它被破壞,關閉應用程序,然後該怎麼做? – 2009-11-05 14:13:02
加上它不會起作用 - 即使您訂閱了該活動,窗口仍會顯示 – 2009-11-05 14:24:45
哦,如果您已經到達此階段,您應該終止應用程序,但是我沒有看到呈現出大問題自定義崩潰對話框。至於你的第二個評論,我只能說它在這裏有效。 – Simon 2009-11-05 15:03:59
我的建議是,不是禁用此對話框,而是向Microsoft註冊,以便您可以看到我們捕獲的錯誤報告!這樣,你可以把這個對話框使用,而不是試圖壓制它。
謝謝,但在這種情況下,它*真*不是解決方案。我需要特別提出的問題 - 在沒有彈出對話框的情況下重啓應用程序的能力。 – 2009-11-06 07:56:02
這將工作,讓你展現自己的自定義對話框:
Application.ThreadException += new ThreadExceptionEventHandler(ThreadExceptionFunction);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionFunction);
這裏是MSDN全樣本:
Thread newThread = null;
// Starts the application.
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)]
public static void Main(string[] args)
{
// Add the event handler for handling UI thread exceptions to the event.
Application.ThreadException += new ThreadExceptionEventHandler(ErrorHandlerForm.Form1_UIThreadException);
// Set the unhandled exception mode to force all Windows Forms errors to go through
// our handler.
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
// Add the event handler for handling non-UI thread exceptions to the event.
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
// Runs the application.
Application.Run(new ErrorHandlerForm());
}
// Programs the button to throw an exception when clicked.
private void button1_Click(object sender, System.EventArgs e)
{
throw new ArgumentException("The parameter was invalid");
}
// Start a new thread, separate from Windows Forms, that will throw an exception.
private void button2_Click(object sender, System.EventArgs e)
{
ThreadStart newThreadStart = new ThreadStart(newThread_Execute);
newThread = new Thread(newThreadStart);
newThread.Start();
}
// The thread we start up to demonstrate non-UI exception handling.
void newThread_Execute()
{
throw new Exception("The method or operation is not implemented.");
}
// Handle the UI exceptions by showing a dialog box, and asking the user whether
// or not they wish to abort execution.
private static void Form1_UIThreadException(object sender, ThreadExceptionEventArgs t)
{
DialogResult result = DialogResult.Cancel;
try
{
result = ShowThreadExceptionDialog("Windows Forms Error", t.Exception);
}
catch
{
try
{
MessageBox.Show("Fatal Windows Forms Error",
"Fatal Windows Forms Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
}
finally
{
Application.Exit();
}
}
// Exits the program when the user clicks Abort.
if (result == DialogResult.Abort)
Application.Exit();
}
// Handle the UI exceptions by showing a dialog box, and asking the user whether
// or not they wish to abort execution.
// NOTE: This exception cannot be kept from terminating the application - it can only
// log the event, and inform the user about it.
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
Exception ex = (Exception)e.ExceptionObject;
string errorMsg = "An application error occurred. Please contact the adminstrator " +
"with the following information:\n\n";
// Since we can't prevent the app from terminating, log this to the event log.
if (!EventLog.SourceExists("ThreadException"))
{
EventLog.CreateEventSource("ThreadException", "Application");
}
// Create an EventLog instance and assign its source.
EventLog myLog = new EventLog();
myLog.Source = "ThreadException";
myLog.WriteEntry(errorMsg + ex.Message + "\n\nStack Trace:\n" + ex.StackTrace);
}
catch (Exception exc)
{
try
{
MessageBox.Show("Fatal Non-UI Error",
"Fatal Non-UI Error. Could not write the error to the event log. Reason: "
+ exc.Message, MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
finally
{
Application.Exit();
}
}
}
// Creates the error message and displays it.
private static DialogResult ShowThreadExceptionDialog(string title, Exception e)
{
string errorMsg = "An application error occurred. Please contact the adminstrator " +
"with the following information:\n\n";
errorMsg = errorMsg + e.Message + "\n\nStack Trace:\n" + e.StackTrace;
return MessageBox.Show(errorMsg, title, MessageBoxButtons.AbortRetryIgnore,
MessageBoxIcon.Stop);
}
如果我不在WinForms應用程序中,該怎麼辦?但是說...控制檯應用程序,我不使用應用程序類? – 2009-11-06 10:57:59
這不是一個編程解決方案,但您可以通過編輯Windows錯誤報告設置來完成此操作。
是的,讓你的應用程序崩潰。 – 2009-11-05 13:49:36