✅ How to avoid blocking until all subscribed handlers complete handling an event
I have the following code:
The event handler
WatcherOnCreated
should have very short execution time to avoid overflowing the FileSystemWatcher
buffer.
I'd like to ask what is the most optimal way of avoiding blocking the FSW when handling the event?
I suppose it is running the handler using async void
, but from what I understand the execution is blocked until the WaitForFileAsync
is awaited, right?
Can I await Task.Yield()
in the beginning of ProcessNewMessageAsync
to avoid blocking sooner?7 Replies
delegates are not great for that because you can only get, at best, one return value from all subscribers.
and async void is... :monkas: don't do it unless forced
why would I want to get a return value from an event handler?
and isn't async void perfectly reasonable for event handlers?
You cant await async void iirc
Yes
the internals of
FileSystemWatcher
just Invoke the event whenever a new file is created
and it does not care whether the handler is async or notright, but it's not blocking anything if its async void
Or does it still need the first
await
within it to really go 'true async'?from my understanding yes, but that is why I'm asking this in the first place
ok then yeah I think that's what task.Yield is for