aspnet / Hosting

[Archived] Code for hosting and starting up an ASP.NET Core application. Project moved to https://github.com/aspnet/Extensions and https://github.com/aspnet/AspNetCore
Apache License 2.0
552 stars 312 forks source link

Make BackgroundService _executingTask protected #1565

Closed IndikaUdagedara closed 5 years ago

IndikaUdagedara commented 5 years ago

When using BackgroundService, I have a requirement to know if the work is completed (I assume it is a common requirement).

This could be achieved if the _executingTask member is protected. Or is there another way to determine completion status?

Tratcher commented 5 years ago

Where would you be checking the status from? A derived class? That class would be implementing the workflow anyways, why not set its own completion flag?

IndikaUdagedara commented 5 years ago

Thanks.

Yes, I guess I could add a flag in the derived class. In my case I have couple of BackgroundServices and there's another service which looks at the completion status of those services and performs an action. I was hoping I could do something like

await Task.WhenAll(service1.ExecutingTask, service2.ExecutingTask, ...)

from this monitoring service to see if the other services have completed.

May be this flag for completion status can be an interface method/property in IHostedService? -- it only has StartAsync and StopAsync methods but no way to indicate if the service is completed. Or do you have any other suggestions to implement this pattern?