c# – 需要增加窗口服务超时

前端之家收集整理的这篇文章主要介绍了c# – 需要增加窗口服务超时前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在启动窗口服务时遇到问题…
因为我的服务的OnStart()事件有很大的负担,它会废弃数据,将其保存到数据库并发送电子邮件.所以我的服务需要增加开始时间,因为defualt超时是30秒……
我已经发布了当我遇到以下异常时我的服务需要额外的时间才能开始.

“Could not start the MyName service on Local Computer. Error 1053: The
service did not respond to the start or control request in a timely
fashion.”

Plz帮帮我……
Thanx提前

解决方法

i have realised that my service will need additional time to start when i face the following exception

在构造函数/启动上执行长运行任务并不好.你应该在sperate线程上开始你的长期运行任务.

服务启动应该是即时的,不应该挂断.

但是,如果你仍然想要,你可以这样做

ServiceBase.RequestAdditionalTime(4000); // add 4 seconds

MSDN

The RequestAdditionalTime method is intended to be called by the overridden OnContinue,OnPause,OnStart,or OnStop methods to request additional time for a pending operation,to prevent the Service Control Manager (SCM) from marking the service as not responding. If the pending operation is not a continue,pause,start,or stop,an InvalidOperationException is thrown.

猜你在找的C#相关文章