My scenario:
- Windows Service .NET 4
- I poll a database for entities.
- When new entities come in they are added to a
BlockingCollection
. - In the service's
OnStart
I create aSystem.Threading.Tasks.Task
whose job is to enumerate theBlockingCollection
(usingGetConsumingEnumerable()
).
The problem I'm having is this:
- When an unhandled exception occurs in the task, I want the exception logged and the service stopped.
- I can't catch exceptions from the task unless I call
Task.Wait()
. - If I call
Task.Wait()
theOnStart
method blocks and the service never finishes starting.
So how can I make this work?