使用Windows服务 [英] work with windows service

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

问题描述

我想创建一个Windows服务,该服务每30分钟执行一次代码.因此,我已将Windows服务添加到我的项目中.然后将一个Timer组件拖到其上并设置其interval属性.然后我在Timer的Tick事件中编写了我的代码.最后在Windows服务的onStart事件中将Timer的Enabled属性设置为true.
我也已经在我的解决方案中添加了一个安装项目并将其安装在我的系统中.但是Windows服务无法正常工作.
请告诉我我的错误在哪里?

i want to create a windows service that perform some codes every 30 minutes.so i have added a windows service to my project. then drag a Timer component to it and set its interval property.then i have written my code in Tick event of Timer.finally set Enabled property of Timer to true in onStart event of windows service.
also i have added a setup project to my solution and install it in my system.but the windows service does not work.
please tell me where is my mistake?

推荐答案

你好朋友...

安装服务后,您需要启动它....

1)转到控制面板..
2)管理工具
3)服务
4)查找您的服务
5)右键单击并启动它

6)现在,您的服务将开始运行....

其他明智的
1)按WindowsKeY + R
2)在运行"中,键入services.msc
3)然后按Enter键,将打开一个服务管理器
4)按照4和5步骤开始服务....


或者您也可以使用设置项目的完成代码来启动服务..

但是在该代码之前,您的服务必须安装在计算机上..
此外,您还可以在Windows启动时自动将服务设为启动状态,因此它将在您重新启动计算机后启动.


希望对您有帮助.

有关服务的基本信息,请参阅本文

简单Windows服务示例 [
Hello Friend...

After installing your service you need to start it....

1) Goto Control Panel..
2) Administrative tools
3) Services
4) Find your service
5) Right Click and start it

6) Now your service will start and working....

other wise
1) Press WindowsKeY + R
2) In Run type it services.msc
3) Then Hit Enter it will open a service manager
4) follow 4 and 5 step to start your service....


or you can also start your service with the finish code of your setup project..

but before that code your service must be installed on your computer..
also you can make your service as start automatically when windows start so it will start after you restart your computer.


I hope this will help you..

For Basic information on services please see this article

Simple Windows Service Sample[^]


代替计时器,请执行以下操作:
Instead of timer do the following:
bool running = true;
int time =0;
while(running)
{
    Thread.Sleep(1000);
    time++;
    if(time>30*60*60)
    {
       time=0;
       // do work here
    }
}


尽量不要使用计时器-而是使用Sleep使您的服务处于睡眠状态.
Try not to use a Timer - instead use Sleep to put your service to bed for a while.
DateTime nextDue = DateTime.Now.AddMinutes(30);
While (stillRunning)
   {
   Thread.Sleep(10000);
   if (DateTime.Now > nextDue)
      {
      ...
      nextDue = DateTime.Now.AddMinutes(30);
      }
   }


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

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