lonix
lonix

Reputation: 20709

When to use .NET Generic Host's Run, Start and WaitForShutdown

I use the .NET Generic Host (to run a BackgroundService). All the examples I've seen (example) use host.Run[Async], so I use that and it works for me.

However there are a number of related lifetime methods:

The docs state:

The difference between Run* and Start* methods is that Run* methods wait for the host to complete before returning, whereas Start* methods return immediately.

I understand that. I tested it and that is exactly what happens.

But the docs continue:

The Run* methods are typically used in console apps, whereas the Start* methods are typically used in long-running services.

I don't understand that. My use case is a long-running service (a "background" service which runs forever until I stop it), but if I use the "Start*" methods then the app exits immediately.

When would that be the preferred option, and how would it be used? I don't understand the difference in these use cases, and there's no real example explaining them. Similarly, when would one use the "WaitForShutdown*" methods?

Upvotes: 1

Views: 1005

Answers (0)

Related Questions