Bugfix/async event handlers return instantly #808
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When trying to test an asynchronous event handler with Raise.Event, the Raise.Event immediately returns on the first thread change in the asynchronous event handler.
This makes testing the behaviour of the event handler almost impossible as you cannot be sure that the eventhandler is finished. That leads to bad test code, for example just waiting for a set amount of time in hopes the event handler has finished.
I fixed this bug by waiting for the event handler to finish with GetAwaiter().GetResult(); which is the safest way to synchronously wait for an asnychronous method to finish.
As handler.DynamicInvoke(eventArguments) already returned a Task independent of the return value of the event handler, it made this bugfix really small.