gpt4 book ai didi

c# - 如何选择 serviceStatus.dwWaitHint 的值?

转载 作者:行者123 更新时间:2023-12-03 07:19:41 24 4
gpt4 key购买 nike

我正在关注本教程:http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.110).aspx创建一个Windows服务。我有一个名为 TaskManager 的类,它使用 Quartz.Net 来管理一堆作业。它有 .Go() (不阻塞)和 .Stop() 方法。如果我理解正确的话,我在服务中需要做的就是

    private TaskManager _taskManager;

public DataPumpService()
{
InitializeComponent();
_taskManager = new TaskManager();
}

protected override void OnStart(string[] args)
{
_taskManager.Go();
}

protected override void OnStop()
{
_taskManager.Stop();
}

但是教程中有一个关于设置服务状态的部分。它并没有真正解释服务状态是什么或者我何时想要设置它。 TaskManager.Stop() 可能需要几秒钟才能完成(在内部它对所有作业调用 IScheduler.Interrupt() ,然后调用 IScheduler.Shutdown(true))。那么我应该设置状态吗?如果是这样,则假设我将代码包含在 the tutorial 的“设置服务状态”部分的第 (1)、(2) 和 (3) 部分中。 ,执行以下操作是否正确(基本上对于上面第一个代码块中的两种方法):

    protected override void OnStop()
{
// Update the service state to Stop Pending.
ServiceStatus serviceStatus = new ServiceStatus();
serviceStatus.dwCurrentState = ServiceState.SERVICE_STOP_PENDING;
serviceStatus.dwWaitHint = 100000;
SetServiceStatus(this.ServiceHandle, ref serviceStatus);

_taskManager.Stop();

// Update the service state to Running.
serviceStatus.dwCurrentState = ServiceState.SERVICE_STOPPED;
SetServiceStatus(this.ServiceHandle, ref serviceStatus);
}

如果这是正确的,那么我需要明智地选择 serviceStatus.dwWaitHint = 100000; 属性,还是应该坚持使用默认值?基本上我不知道这个值的用途是什么......

最佳答案

@HansPassant

The way the ServiceBase class already takes care of the service statusis good enough in 99.9% of the cases.You should not need it, 30 seconds (the default) is good enough to get 99.9% of all servicesstarted/stopped.

但是如果您需要处理长时间运行的关闭, documentation说有关

dwWaitHint

The estimated time required for a pending start, stop,pause, or continue operation, in milliseconds. Before the specifiedamount of time has elapsed, the service should make its next call tothe SetServiceStatus function with either an incremented dwCheckPointvalue or a change in dwCurrentState. If the amount of time specifiedby dwWaitHint passes, and dwCheckPoint has not been incremented ordwCurrentState has not changed, the service control manager or servicecontrol program can assume that an error has occurred and the serviceshould be stopped. However, if the service shares a process with otherservices, the service control manager cannot terminate the serviceapplication because it would have to terminate the other servicessharing the process as well.

dwCheckPoint

The check-point value the service increments periodicallyto report its progress during a lengthy start, stop, pause, orcontinue operation. For example, the service should increment thisvalue as it completes each step of its initialization when it isstarting up. The user interface program that invoked the operation onthe service uses this value to track the progress of the serviceduring a lengthy operation. This value is not valid and should be zerowhen the service does not have a start, stop, pause, or continueoperation pending.

这澄清了 walk-through 中的脚注.

The Service Control Manager uses the dwWaitHint and dwCheckpointmembers of the SERVICE_STATUS structure to determine how much time towait for a Windows Service to start or shut down. If your OnStart andOnStop methods run long, your service can request more time by callingSetServiceStatus again with an incremented dwCheckPoint value.

基于此,我这样编写了我的停止代码。请注意,我运行了一些非常长的任务,终止它们确实不是一个好主意,因此等待时间很长。

//set the status to pending close
var serviceStatus = new ServiceStatus
{
dwCurrentState = ServiceState.SERVICE_STOP_PENDING,
dwWaitHint = 120000//two minutes wait time
};
SetServiceStatus(this.ServiceHandle, ref serviceStatus);

Engine.Cancel();

while (Engine.IsRunning())
{
System.Threading.Thread.Sleep(1000);
serviceStatus.dwCheckPoint++;//updating the checkpoint so I don't get terminated
SetServiceStatus(this.ServiceHandle, ref serviceStatus);
}

关于c# - 如何选择 serviceStatus.dwWaitHint 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26241389/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com