I have a Windows Service written in C# that periodically fires off background jobs. Typically, at any given time, several dozen heavily I/O bound Tasks (downloading large files, etc) are running in parallel. The service runs on a relatively busy web server (necessary for now), and I think it could benefit greatly in terms of thread conservation to use asynchronous APIs as much as possible.
Most of this work is done. All jobs are now fully async (leveraging HttpClient, etc.), as is the main job loop (with heavy doses of Task.Delay). All that's left is to figure out how to correctly and safely fire up the main loop from the service's OnStart. Essentialy, it's the much-warned-about calling-async-from-sync dilemma. Below is what I have so far (grossly simplified).
in Program.cs:
static void Main(string[] args) {
TaskScheduler.UnobservedTaskException += (sender, e) => {
// log & alert!
e.SetObserved();
};
ServiceBase.Run(new MyService());
}
in MyService.cs:
protected override void OnStart(string[] args) {
_scheduler.StartLoopAsync(); // fire and forget! will this get me into trouble?
}
It's that call to StartLoopAsync
that concerns me. I can't simply Wait()
on the returned Task because OnStart needs to return relatively quickly. (Job loops need to run on a separate thread.) A couple thoughts come to mind:
- Am I well covered as far as unobserved exceptions by placing that handler in Main?
- Would there be any benefit to using Task.Run, something like
Task.Run(() => _scheduler.StartLoopAsync().Wait());
? - Would there be any benefit to calling
_scheduler.StartLoopAsync().ConfigureAwait(false)
here? (I'm doubting it since there's noawait
here.) - Would there be any benefit to using Stephen Cleary's AsyncContextThread in this situation? I haven't seen any examples of using this, and since I'm starting an infinite loop I don't know that syncing back up to some context is even relevant here.
AsyncContextThread
example next week. – HillariOnStart
and then join to it inOnStop
. – Hillari