I have a class that fires an event at a fairly rapid frequency (every 100ms). To avoid slowing down this process, event handlers should hand off the processing to some kind of background task/thread/worker (as opposed to doing it within the event handler).
What’s the best approach? Should I use Task.Factory.StartNew within the event handler? Will there be an overhead of creating tasks this rapidly (e.g. 5 event handlers will create 5 tasks every 100ms)? Or could I use a BackgroundWorker (instantiated in a subscriber’s constructor), and in the event handler call the .RunAsync method?
Two ways:
Task.Factory.StartNew. This method relies on the ThreadPool to minimize the task creation cost. This should be ok in most cases.