c# - Async method throws exception instantly but is swallowed when async keyword is removed -


I am getting some behavior, which I can not understand when throwing an exception in async methods.

The following code will throw an exception when calling the ThrowNow method immediately. If I comment on that line and throw the exception directly, the exception is swallowed in an unaffected event handler and not raised.

  public stable async zero ThrowNow (Exception pre) {east; } Public stable async work TestXSync () {ThrowNow (new system .expression ("test")); // throws instances immediately / throwing new system Option ("test"); // Exception has been swallowed, is not raised in unaffected event. Waiting (1000); } Zero main () {var task = TestExAsync (); }   

If I remove the async keyword from the ThrowNow method, then it has been swallowed again.

I thought that async methods run in a synchronous way until they reach a blocked method. In this situation, it seems that it is behaving asynchronously by removing the async keyword.

I think async methods run in a synchronous manner until access to a blocked method.

They do, but they still know that they are executing within an asynchronous method.

If you throw an exception directly from an async zero method, the async system knows that there is no way to see that exception - it does not throw the collar back Because the throw exception methods in Async are propagated only through actions (the returned function becomes defective.) This first time blocked waiting to throw the expression directly first Throwing will be strange for the exception, but the exception The latter can be handled differently. / P>

As far as I know, an exception to be thrown by an async zero method has been passed directly from the synchronization context, if any (one continuation on the synchronization context Is posted which throws an exception.) In a simple console app, is not a synchronization context, so instead it is thrown in the form of an unchanged exception.

If you change the zero method to return jobs , then instead you will have an exception that could Have been seen, but not (because you are not using the return value in TestExAsync .)

Does this make any sense? I know if you want more explanation - all this is fraudulent (and I do not know how well it is documented).

Edit: I have a bit documentation, C # 5 spec section 10.15.2:

if the return type of the async function is zero , The evaluation is different from the following way: Because there is no work returned, the function instead converts the completion of the synchronization reference of the current thread and the exceptions The exact definition of synchronization reference is implementation-dependent , But presently Security "is a representation of the" running. The synchronization reference is notified when an exception exception is created for successfully completing or throwing a zero-returning async function.

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -