2009-10-19 3 views
3

이 코드 조각은 잘못 설계 되었습니까? 원래는 finally 블록에 AppDomain.Unload 하나만있었습니다. 이것은 UnhandledException이 실행되는 동안 다른 스레드가 AppDomain에서 계속 실행될 수 있다는 불행한 부작용이있었습니다. 다른 것들 중에서 사용자 입력을 사용하므로 컴퓨팅 규모에서 매우 느립니다 (평균 실제 런타임은 1 분 이상일 수 있음). 잠재적으로 다른 예외가 발생합니다. 일반적으로 더 많은 문제를 야기합니다. 나는이 일을하는 '더 나은'방식을 생각하고있다. 그래서 나는 이것을 이렇게 제출했다. 네 마음을 빌려줘.잘못된 AppDomains를 정상적으로 처리하려면 어떻게해야합니까?

참고 : 나는 여기에도 동기화 문제가 있다는 것을 깨달았습니다. 예, 나는 그들이 무엇인지 알고, 집중할 수 있습니다.

mainApp = AppDomain.CreateDomain(ChildAppDomain, null, AppDomain.CurrentDomain.SetupInformation); 
try 
{ 
    mainApp.ExecuteAssembly(Assembly.GetEntryAssembly().Location); 
    finished = true; 
} 
catch (Exception ex) 
{ 
    AppDomain.Unload(mainApp); 
    mainApp = null; 
    UnhandledException(this, new UnhandledExceptionEventArgs(ex, false)); 
} 
finally 
{ 
    if (mainApp != null) 
    { 
     AppDomain.Unload(mainApp); 
     mainApp = null; 
    } 
} 

// ... 

void UnhandledException(object sender, UnhandledExceptionEventArgs e) 
{ 
    if (mainApp != null) 
    { 
     AppDomain.Unload(mainApp); 
     mainApp = null; 
    } 
    // [snip] 
} 

답변

2

필자는 중복되지 않도록 노력할 것입니다. 그리고 당신은 처음에했던 것처럼 finally 블록의 appdomain을 정리하는 것만으로이 작업을 할 수 있습니다. 아이디어는 처리되지 않은 예외가 발생하면 변수에 배치하고 appdomain을 종료 한 후에 처리하는 것입니다.

Exception unhandledException = null; 
try 
{ 
    ... 
} 
catch (Exception ex) 
{ 
    unhandledException = ex; 
} 
finally 
{ 
    CleanupAppDomain(mainApp); 
} 

if (unhandledException != null) 
    UnhandledException(this, new UnhandledExceptionEventArgs(unhandledException, false)); 
관련 문제