Windows Service Variable Lifetime

流过昼夜 提交于 2019-12-08 06:38:24

A Windows Service doesn't introduce any more lifetime complexities than any other program. The only difference is that it runs quietly in the background.

A .NET application implementing a Windows Service inherits the ServiceBase class and overrides the OnStart method. That's the extent of the API. The OnStart method is in charge of everything else - if it simply exits, then the service exits as well. If it spawns a new background thread or task to perform its work, then that thread will continue to exist.

A variable defined in OnStart will go out of scope when that method exits. One defined in the class scope, or in the scope of a class that's called from OnStart, will be tied to the lifetime of that class instance.

Without knowing exactly where you're defining your variables, we can't tell you what their lifetime will be, but chances are that running as a Windows Service won't be the deciding factor.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!