0

취소 :오류 : 작업이 내가 취소 토큰 비동기 쿼리를 수행하는이 코드를 사용하고

작업이 취소 얻을
var _client = new HttpClient(/* some setthngs */); 

_client.GetAsync(someUrl, cancellationToken).ContinueWith(gettingTask => { 
    cancellationToken.ThrowIfCancellationRequested(); 
    SomeStuffToDO(); 
    }, TaskScheduler.FromCurrentSynchronizationContext()); 
}, TaskScheduler.FromCurrentSynchronizationContext()); 

그러나이, cancellationToken.ThrowIfCancellationRequested();는 예외가 발생합니다. 나는이 줄이이 물건에 있어야한다는 것을 안다. 그러나 개발 환경에서는 예외적으로 Visual Studio가 중단됩니다. 어떻게 이것을 피할 수 있습니까?

var _client = new HttpClient(/* some setthngs */); 

_client.GetAsync(someUrl, cancellationToken).ContinueWith(gettingTask => { 
    try { 
    cancellationToken.ThrowIfCancellationRequested(); 
    SomeStuffToDO(); 
    } 
    catch (...) { ... } 
    finaly { ... } 
    }, TaskScheduler.FromCurrentSynchronizationContext()); 
}, TaskScheduler.FromCurrentSynchronizationContext()); 

그러나 _client.GetAsync(someUrl, cancellationToken)도 취소 예외가 발생할 수 있습니다, 그래서 당신은 try-으로 (또는 포함하는 방법을 기다려온되는 경우) 그 전화를 포장해야합니다

+0

VS가 멈추고 "예외"대화 상자를 보여주는 "비주얼 스튜디오 중단"을 의미합니까? 개발 또는 런타임, 예외를 처리하지 않으면 앱이 실패하게됩니다. 이를 피하기 위해 예외를 잡아서 처리해야합니다. –

+0

@ G.Stoynev 예 VS가 중지되고 "예외"대화 상자가 표시됩니다. 그렇다면 어디에서 예외를 처리 할 수 ​​있습니까? 메인 스레드 또는 비동기? –

답변

1

는이 같은 람다 내에서 처리해야 잡기.