C# 自己写了一个windows服务程序,运行时错误: 错误 1053: 服务没有及时响应启动或控制请求。(推荐) | 您所在的位置:网站首页 › 服务错误1053 › C# 自己写了一个windows服务程序,运行时错误: 错误 1053: 服务没有及时响应启动或控制请求。(推荐) |
来自:http://zhidao.baidu.com/question/348636017.html 提问者: lcy2008lcy 问题: OnStart里面要放一个运行时间很短的方法,能够成功启动该服务,当OnStart里面放一个运行时间很长的方法(比如超过2小时的方法)时就出现上面那个错误, 1、请高手给个编写服务的教程或源码 2、怎样才能在服务里面运行很费时间的方法, 问题原因: 解析1=>当您停止或暂停托管 Microsoft Windows 服务时,如果该服务停止或暂停过程所花的时间比已配置的时间长,您会收到错误信息 原因 ServiceBase 类直接从 Advapi32.dll 文件的 ScDispatcherLoop 中定义的 Service 命令处理程序调用 OnStop 方法。30 秒之后,如果 ScDispatcherLoop 线程未准备好从服务控制管理器接收新的服务命令,则 Windows 服务控制器会将此服务标记为“超时”。因此,就会收到此错误信息 解析2=>这个错误是服务应答超时,估计是同时运行的程序较多,系统忙,所以各个程序分得的时间片较少,而其中某个服务程序写的不好,没有及时SetServiceStatus而造成的。 解决方法: 1.你可能需要在onStart()方法里另起一个线程,在这个线程里可以while(true). protected override void OnStart(string[] args) { // TODO: Add code here to start your service. if (threadforwork == null) { threadforwork = new Thread(workFunction); } threadforwork.IsBackground = true; threadforwork.Start(); } 在onStop()里面将线程杀掉 protected override void OnStop() { // TODO: Add code here to perform any tear-down necessary to stop yourservice. if (threadforwork != null) { if (threadforwork.ThreadState == System.Threading.ThreadState.Running) { threadforwork.Abort(); } } } 2.使用Timer 定义一个操作类,添加一个Operate.cs文件,在该文件中添加操作方法 在Service1该类中添加: Timer doTimer; Operate op1 = new Operate(); Timer是用来循环执行Operate.cs文件中操作方法的 在OnStart方法中输入: //设置循环时间,以及循环执行方法 int doTimerNum = Convert.ToInt32(RestartMinute) * 60 * 1000; doTimer = new Timer(doAutoRestartNetReg, null, 0, doTimerNum); 手工建立一个方法,用来被Timer执行: private void doAutoRestartNetReg(object values) { //循环调用方法 op1.RestartAppPool(AppPoolName, LogType); } |
CopyRight 2018-2019 实验室设备网 版权所有 |