惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
V
V2EX
博客园_首页
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recent Announcements
Recent Announcements
博客园 - 司徒正美
Microsoft Security Blog
Microsoft Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Latest news
Latest news
Vercel News
Vercel News
The Register - Security
The Register - Security
T
The Exploit Database - CXSecurity.com
S
Schneier on Security
N
Netflix TechBlog - Medium
WordPress大学
WordPress大学
小众软件
小众软件
L
Lohrmann on Cybersecurity
GbyAI
GbyAI
P
Privacy & Cybersecurity Law Blog
T
Tor Project blog
AWS News Blog
AWS News Blog
美团技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
K
Kaspersky official blog
B
Blog RSS Feed
G
Google Developers Blog
量子位
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
Scott Helme
Scott Helme
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
I
Intezer
雷峰网
雷峰网
Martin Fowler
Martin Fowler
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Blog — PlanetScale
Blog — PlanetScale
IT之家
IT之家
F
Full Disclosure
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 【当耐特】
The Hacker News
The Hacker News
U
Unit 42
S
SegmentFault 最新的问题
I
InfoQ
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
宝玉的分享
宝玉的分享
罗磊的独立博客
Spread Privacy
Spread Privacy
C
CERT Recently Published Vulnerability Notes

博客园 - sdxd.bgl

将SQL Server中的数据显示到SharePoint中 拓展训练总结 在PowerShell中,如何获取SharePoint的默认数据库服务器 在SharePoint页面中如何显示来自其他网站的List 恢复导出XSLT List View WebPart 实战部署FAST Search Server 2010 for SharePoint Sharepoint开发中代码运行权限级别 在InfoPath中如何获取当前用户的信息(Profile) 晒晒我的家乡 关于模式的思考 小心委托 利用System.Linq.Expressions实现四则运算计算器(三) 利用System.Linq.Expressions实现四则运算计算器(二) 利用System.Linq.Expressions实现四则运算计算器(一) 初识System.Linq.Expressions 请拆招:将两个已排序集合分解成两个独立部分的集合和一个共有部分的集合? 用javascript实现下拉列表的自动筛选功能 - sdxd.bgl - 博客园 我的生活 电脑是个神奇的东西!
如何实现Windows服务
sdxd.bgl · 2007-02-16 · via 博客园 - sdxd.bgl

 一、 遇到什么问题?
有些软件,需要每隔一定时间做一些相同的事情,或者作为网络服务,像IIS、SQL Server等这样的软件。此种情况下,往往需要让软件在没有用户干预的情况下在服务器上运行。与此种程序交互往往需要通过网络协议进行。或者程序根本就不需要与用户交互。这种程序往往称为后台程序,或后台服务。

二、 Windows服务能做些什么?
Windows服务是这些后台程序、后台服务的正规名词。Windows服务的运行可以在没有用户干预的情况下,在后台运行,没有任何界面。通过Windows服务管理器进行管理。服务管理器也只能做些简单的操作:开始,暂停,继续,停止。
Windows服务和普通的Windows窗体应用程序类型,只是没有了界面,连最简单的MessageBox也不能弹出来。所以不要试图在Windows服务里通过这种方式来提示用户。因为可能根本没有用户登录计算机。
Windows服务的特点:
 在后台运行
 没有用户交互
 可以随Windows启动而启动
三、 如何实现Windows服务?
下面按“隔一定时间做一些相同的事情”的服务为例,说明Windows服务如何实现。
先按普通Windows程序设计好你的程序逻辑。
建立一个空白解决方案WindowsService.sln
添加Windows类库项目ServiceBusiness.csproj
将Class1.cs改名为ServiceBusiness.cs
添加一个方法Dothings(),这个方法用来每隔一段时间调用一次,做些周期性的事情。

using System;namespace ServiceBusiness
{
    
public class ServiceBusiness
    {
        
public void Dothings()
        {
            
//隔一段时间调用一次
        }
    }
}

向解决方案添加一个WindowsService.csproj

将Service1.cs重命名为Service.cs
给WindowsService添加ServiceBusiness项目引用
打开Service.cs代码视图,向Service类添加成员
ServiceBusiness.ServiceBusiness serviceBusiness;
在构造函数里面对serviceBusiness实例化
serviceBusiness = new ServiceBusiness.ServiceBusiness();
在using位置添加System.Theading
using System.Threading;
给Service类添加计时器
Timer serviceTimer;
添加TimeCallback方法,用于计时器调用

        public void TimerCallback(object obj)
        {
            
//隔一段时间调用一次
            serviceBusiness.Dothings();
        }

在OnStart()方法中添加方法,用于启动计时器
serviceTimer = new Timer(new TimerCallback(TimerCallback), state, 0, period);
此处,state用于保存状态,如果不需要,保存状态,可以传入null。第三个参数0表示立即调用TimerCallback方法,如果不需要立即调用,可以传入period。period是计时器的计时间隔,单位为毫秒。
重载 OnPause ()和OnContinue ()方法,对计时器进行控制。
Service.cs代码如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Threading;namespace WindowsService
{
    
public partial class Service : ServiceBase
    {
        Timer serviceTimer;
        ServiceBusiness.ServiceBusiness serviceBusiness;
        
int period;
        
object state;
        
public Service()
        {
            InitializeComponent();
            serviceBusiness 
= new ServiceBusiness.ServiceBusiness();
        }
protected override void OnStart(string[] args)
        {
            
//启动timer
            period = ServiceSettings.Default.ServiceTimerIntervalSecond * 1000;
            serviceTimer 
= new Timer(new TimerCallback(TimerCallback), state, 0, period);
        }
protected override void OnStop()
        {
            
//停止计时器
            serviceTimer.Change(Timeout.Infinite, Timeout.Infinite);
        }
protected override void OnContinue()
        {
            
//重新开始计时
            serviceTimer.Change(0, period);
        }
protected override void OnPause()
        {
            
//停止计时器
            serviceTimer.Change(Timeout.Infinite, Timeout.Infinite);
        }
        
        
public void TimerCallback(object obj)
        {
            
//隔一段时间调用一次
            serviceBusiness.Dothings();
        }

    }
}

打开Program.cs文件
为了调试方便,在Main方法中,直接加入调用Dothings()。这样就可以在IDE中方便调试程序。

using System.Collections.Generic;
using System.ServiceProcess;
using System.Text;namespace WindowsService
{
    
static class Program
    {
        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>
        static void Main(string[] args)
        {
#if DEBUG
            ServiceBusiness.ServiceBusiness serviceBusiness 
= new ServiceBusiness.ServiceBusiness();
            
//在调试模式下,直接调用
            serviceBusiness.Dothings(args);
#else
            ServiceBase[] ServicesToRun 
= new ServiceBase[] { new Service() };
            ServiceBase.Run(ServicesToRun);
#endif
        }
    }
}