zoukankan      html  css  js  c++  java
  • Window Service 程序模板

    写一个windows 的后台服务,后台服务需要每隔一段时间定期轮询数据库。

    刚开始计划在Service 的OnStart事件中,起一个新的后台线程,由这个后台线程进行数据库轮询。后来发现用Timer 定期触发更好!

    代码例子如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.ServiceProcess;
    using System.Text;
    using System.Threading;
    using System.Timers;

    namespace EDS.SMAP.WinService
    {
        
    /// <summary>
        
    /// 服务地图后台服务,实现2个功能
        
    /// </summary>
        public partial class Service1 : ServiceBase
        {

            
    private System.Timers.Timer timer1;

            
    public Service1()
            {
                InitializeComponent();

                
    this.timer1 = new System.Timers.Timer();
                
    this.timer1.Interval = 3600000;
                
    this.timer1.Elapsed +=new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
                
    this.timer1.Enabled = false;

            }

            
    protected override void OnStart(string[] args)
            {
                
    // TODO: Add code here to start your service.
                this.timer1.Enabled = true;


            }

            
    protected override void OnStop()
            {
                
    // TODO: Add code here to perform any tear-down necessary to stop your service.
                this.timer1.Enabled = false;
            }

            
    /// <summary>
            
    /// 定时器定期触发工作
            
    /// </summary>
            
    /// <param name="sender"></param>
            
    /// <param name="e"></param>
            private void timer1_Elapsed(object sender,System.Timers.ElapsedEventArgs e)
            {

            }

        }
    }
  • 相关阅读:
    java读写文件
    idea文件全部变红, 文件全部红色
    PowerDesigner连接MySQL数据库
    mysql 使用ip地址连接不上;MySQL 可以用localhost 连接,但不能用IP连接的问题,局域网192.168.*.* 无法连接mysql
    powerdesigner连接MySQL数据库时出现Non SQL Error : Could not load class com.mysql.jdbc.Driver
    JSP的九大对象和四大作用域
    C#面试问题及答案
    数据库面试题及答案
    多态的深入理解
    面向对象编程----继承---笔记
  • 原文地址:https://www.cnblogs.com/yhnxuhbgx/p/1282204.html
Copyright © 2011-2022 走看看