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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - sdxd.bgl

将SQL Server中的数据显示到SharePoint中 - sdxd.bgl 拓展训练总结 在PowerShell中,如何获取SharePoint的默认数据库服务器 在SharePoint页面中如何显示来自其他网站的List 恢复导出XSLT List View WebPart - sdxd.bgl 实战部署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
        }
    }
}