我有一個靜態類在其構造函數中創建了幾個工作線程。如果在工作人員創建之前發生異常,我的Application.ThreadException處理程序(用於在發生不可恢復的錯誤時關閉應用程序)會正常觸發,並且一切正常。一旦創建了第一個工作線程,除了處理程序觸發外,我還會收到一個「MYAPP遇到問題並需要關閉,我們對此造成的不便表示抱歉。」 MS錯誤報告對話框。在多線程應用程序中處理靜態構造函數異常
在這個特定的實例中,我可以重新排列代碼以最後創建線程(在任何可能觸發異常的資源初始化/訪問問題之後),但這只不過是對問題的一種繃帶而不能給我有關實際發生的任何信息。
希望我已經從我的應用程序中刪除了足夠的代碼,以顯示我在這裏要做的事情。
class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
try
{
Application.Run(theForm);
theForm.Dispose();
}
catch (Exception e)
{
//doing this to use the same handler here and for Application.ThreadException
ThreadExceptionEventArgs argsEx = new ThreadExceptionEventArgs(e);
FatalExceptionHandler(null, argsEx);
}
finally
{
MyStaticClass.KillThreads();
}
}
public static void FatalExceptionHandler(object sender, System.Threading.ThreadExceptionEventArgs ex)
{
Exception e = ex.Exception;
try
{
//lots of stuff to give more useful error messages for known problems
//and display them in a messagebox.
}
// if anything went wrong scraping the exception text for formatting, show the raw value.
catch
{
MessageBox.Show(e.Message);
return;
}
// after showing the MessageBox, close out the app.
finally
{
System.Environment.Exit(1);
}
}
}
class MyStaticClass
{
static MyStaticClass()
{
myThread = new Thread(new ThreadStart(SomeMethod));
//if this exception is thrown everything works normally
//Throw new Exception("KABOOM");
myThread.Start();
//if this exception is thrown a windows error reporting dialog appears
//along with the messagebox from program.FatalExcetion handlder
//Throw new Exception("KABOOM");
}
public void KillThreads()
{
//clean up the worker threads
}
}
我認爲構造函數總是被調用,最遲在它的成員/方法/屬性被訪問時調用。 – 2009-09-21 18:36:35
這是正確的,但是訪問成員的「if和when」時,它仍然沒有解決a)構造函數運行於哪個線程上,b)運行時相對於應用程序生存期。下面是我剛剛制定的一個經驗法則,可以幫助您:如果靜態構造函數中的代碼無法在不更改程序行爲的情況下連續運行兩次,請將代碼移至靜態初始化方法。 – 2009-09-21 18:44:57
如果我將所有代碼從靜態構造函數移動到初始化方法,是否有理由保持構造函數? – 2009-09-21 19:50:18