Windows 服务变量生存期 [英] Windows Service Variable Lifetime

查看:46
本文介绍了Windows 服务变量生存期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解 Windows 服务上的变量生存期.

I would like to understand the Variable lifetime on a Windows service.

我有一个 SQL 表,其中一个临时表存储了我的应用程序的 INFLOW 和 OUTFLOW 记录.

I have a SQL table where a temporary table stores both INFLOW and OUTFLOW records for my application.

我声明了两个像下面这样的全局变量,并为这个变量分配了临时表中的行 ID.int iTempKeyInflow = 0;int iTempKeyOutflow = 0;

I declared two global variables like below and to this variable i assign the Row Id from the temporary table. int iTempKeyInflow = 0; int iTempKeyOutflow = 0;

随着时间的流逝,我是否会丢失服务在上次运行中处理的这些值.

On elapsed time do i loose this values that the service was handling in the previous run.

基本上如果定时器在一个周期的执行完成之前到期,服务周期是否仍然在后台运行.那么我可以在一秒钟内处理表中的 100 条记录吗?

Basically if the timer expires before the completion of execution of a cycle, does the service cycle still run in background. So can i handle 100 records in a second from the table?

推荐答案

Windows 服务不会比任何其他程序引入更多的生命周期复杂性.唯一的区别是它在后台安静地运行.

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.

实现 Windows 服务的 .NET 应用程序继承 ServiceBase 类并覆盖 OnStart 方法.这就是 API 的范围.OnStart 方法负责其他所有事情——如果它只是退出,那么服务也会退出.如果它产生一个新的后台线程或任务来执行它的工作,那么该线程将继续存在.

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.

OnStart 中定义的变量将在该方法退出时超出范围.在类作用域中定义的,或者在从 OnStart 调用的类的作用域中定义的,将与该类实例的生命周期相关联.

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.

在不知道您在何处定义变量的情况下,我们无法告诉您它们的生命周期是什么,但作为 Windows 服务运行的可能性不是决定因素.

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.

这篇关于Windows 服务变量生存期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆