2014-07-17 1 views
3

http://blogs.msdn.com/b/pfxteam/archive/2009/05/31/9674669.aspx에 따르면 처리되지 않은 예외가있는 작업은 응용 프로그램을 손상시킬 수 있습니다.처리되지 않은 예외가있는 작업이 .NET 4.0을 대상으로하는 동안 끊어지지 않음

Task unhandled exceptions에서 대답은 .NET 4.5에서 동작이 응용 프로그램을 손상시키지 않도록 변경되었음을 나타내며 MSVS 2013에서 충돌 동작을 재현하려고하며 대상을 지정하는 동안 응용 프로그램을 중단시킬 수 없습니다 .NET 4.5 (예상)이지만 .NET 4.0을 대상으로하더라도 약한 참조를 통해 작업이 더 이상 존재하지 않음을 확인한 후에도 응용 프로그램이 계속 유지됩니다.

class Program 
{ 
    private static WeakReference _ThrowingExceptionOnATaskRun() 
    { 
     Task t = Task.Factory.StartNew(() => 
     { 
      Console.WriteLine("Throwing exception in a task!"); 
      throw new NotImplementedException("Not implemented"); 
     }); 

     return new WeakReference(t); 
    } 

    static void MyMain() 
    { 
     Console.WriteLine("Main start"); 

     WeakReference weakReference = _ThrowingExceptionOnATaskRun(); 

     CheckIfAlliveForceGc(weakReference); 
     CheckIfAlliveForceGc(weakReference); 
     CheckIfAlliveForceGc(weakReference); 
     CheckIfAlliveForceGc(weakReference); 

     Console.WriteLine("Enter something:"); 
     string userInput = Console.ReadLine(); 
     Console.WriteLine("You entered : {0}", userInput); 
     Console.WriteLine("Done..."); 
     Console.Read(); 
    } 

    private static void CheckIfAlliveForceGc(WeakReference weakReference) 
    { 
     Console.WriteLine("Is reference alive : {0}", weakReference.IsAlive); 
     Thread.Sleep(2000); 
     GC.Collect(); 
     GC.WaitForPendingFinalizers(); 
    } 

    static void Main(string[] args) 
    { 
     MyMain(); 
    } 
} 

출력

당신은 이전 동작 유지하기 위해 응용 프로그램 /의 Web.config에 구성 요소를 추가 할 필요가
Main start 
Is reference alive : True 
Throwing exception in a task! 
Is reference alive : False 
Is reference alive : False 
Is reference alive : False 
Enter something: 
hello 
You entered : hello 
Done... 

답변

0

:

<runtime> 
    <ThrowUnobservedTaskExceptions enabled="true"/> 
</runtime> 
관련 문제