I'd like to do something like this:
public async Task<int> DoWork(int parameter) {
try {
await OperationThatMayCompleteSynchronously(parameter);
} catch(Exception) e {
if(completedSynchronously)
doSyncThing();
else
doAsyncThing();
}
}
Note: I'm running tasks on the thread pool, so there is no async context.
I'd like to be able to tell the difference between an exception thrown immediately, and I'm still on the calling thread (e.g. parameter
is invalid causing the function to abort), and an exception thrown when the async task completes, and I'm on some other random callback thread (e.g. network failure)
I can work out how I might achieve this if I didn't use await
, and just used ContinueWith
on the async operation, but is it possible using await
?