c# - async await exception catching - which thread am I on? -


i'd 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 thread pool, there no async context.

i'd able tell difference between exception thrown immediately, , i'm still on calling thread (e.g. parameter invalid causing function abort), , exception thrown when async task completes, , i'm on other random callback thread (e.g. network failure)

i can work out how might achieve if didn't use await, , used continuewith on async operation, possible using await?

store task in variable:

var task = operationthatmaycompletesynchronously(parameter); //may throw 

then await it:

await task; //may throw 

that way can differentiate between 2 origins potential exception.

note, async methods never throw directly. pass exceptions through task return. true "validation" throws execute before first await.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -