2014-03-12 98 views
0

所以基本上我想測試c#中的任務。任務錯誤處理 - Visual Studio停止

我的代碼如下:

Task tm = new Task(Test); 
    tm.ContinueWith(ExceptionHandler, TaskContinuationOptions.OnlyOnFaulted); 
    tm.Start(); 


    void ExceptionHandler(Task task) 
    { 
     var ex = task.Exception; 
     ex.Handle((x) => 
      { 
       if(x is PictureNotRecognized) 
       { 
        MessageBox.Show(String.Format("An exception occured" + Environment.NewLine + "Program aborted: {0}" + Environment.NewLine + "Step: {1}" + Environment.NewLine + "Error Message: {2}", (x as PictureNotRecognized).Aborted, (x as PictureNotRecognized).CurrentStep, (x as PictureNotRecognized).Message)); 
        return true; 
       } 
       return false; 
      }); 
    } 

    void Test() 
    { 
     throw new Exception(); 
    } 

所以,當我運行應用程序,它拋出異常和Visual Studio停止。只有當我再次按下F5時,它纔會繼續並跳轉到ExceptionHandler。我怎樣才能停用Visual Studio中的這個?它不應該破壞程序,如果異常是由用戶創建的throw new XXXX

回答

相關問題