I have code that I downgrade from .NET 4.5 fine async and await keywords to .NET 4.0. I use ContinueWith to create a continuation similar to how await works.
Basically, my old code is:
var tokenSource = newCancellationTokenSource(); var myTask = Task.Run(() => { return MyStaticClass.DoStuff(tokenSource.Token); }, tokenSource.Token); try { var result = await myTask; DoStuffWith(result); } catch (OperationCanceledException) {
(As expected, MyStaticClass.DoStuff(token) regularly calls token.ThrowIfCancellationRequested() .)
My new code is as follows:
var tokenSource = new CancellationTokenSource(); try { Task.Factory.StartNew(() => { return MyStaticClass.DoStuff(tokenSource.Token); }, tokenSource.Token) .ContinueWith(task => { var param = new object[1]; param[0] = task.Result;
However, OperationCanceledException never throws. What's happening? Where can I place a try / catch block?
source share