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

推荐订阅源

T
Troy Hunt's Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
Threat Research - Cisco Blogs
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Schneier on Security
Schneier on Security
Scott Helme
Scott Helme
L
LINUX DO - 热门话题
Help Net Security
Help Net Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Palo Alto Networks Blog
P
Privacy International News Feed
AWS News Blog
AWS News Blog
Forbes - Security
Forbes - Security
N
News and Events Feed by Topic
L
LINUX DO - 最新话题
A
Arctic Wolf
Hacker News: Ask HN
Hacker News: Ask HN
P
Proofpoint News Feed
N
News and Events Feed by Topic
S
Security @ Cisco Blogs
Cyberwarzone
Cyberwarzone
Google Online Security Blog
Google Online Security Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
PCI Perspectives
PCI Perspectives
Know Your Adversary
Know Your Adversary
The Hacker News
The Hacker News
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Tenable Blog
S
Security Affairs
P
Privacy & Cybersecurity Law Blog
W
WeLiveSecurity
Jina AI
Jina AI
The GitHub Blog
The GitHub Blog
S
Securelist
AI
AI
Latest news
Latest news
T
The Blog of Author Tim Ferriss
Application and Cybersecurity Blog
Application and Cybersecurity Blog
C
Cybersecurity and Infrastructure Security Agency CISA
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
C
CXSECURITY Database RSS Feed - CXSecurity.com
美团技术团队
G
GRAHAM CLULEY
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Hacker News - Newest:
Hacker News - Newest: "LLM"
C
Cisco Blogs
V
Visual Studio Blog
L
LangChain Blog

博客园 - 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
        }
    }
}