玄铁剑

成功的途径:抄,创造,研究,发明...
posts - 128, comments - 42, trackbacks - 0, articles - 174

Windows Service定时执行

Posted on 2010-07-11 14:17 玄铁剑 阅读(876) 评论(0)  编辑 收藏 引用 所属分类: C# Winform

using System;
using System.IO;
using System.Text;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Remoting;
using System.Threading;
using Kingle.ExchangeCore;
using DevPack.Core;

namespace Kingle.NetService
{
    public partial class MainExchange : ServiceBase
    {

        System.Timers.Timer Ticket = new System.Timers.Timer();
        string filePath = System.AppDomain.CurrentDomain.BaseDirectory + @"Configration.ini";

        string WWW_site = "";
        string Local_Site = "";

        public MainExchange()
        {
            InitializeComponent();
        }

        private void writeLog(string strMsg, EventLogEntryType MsgType)
        {
            try
            {
                if (EventLog.SourceExists("KingleService") == false)
                {
                    EventLog.CreateEventSource("KingleService", "KingleServiceLog");
                }

                EventLog mylog = new EventLog();
                mylog.Source = "KingleService";
                mylog.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 30);
                mylog.WriteEntry(DateTime.Now.ToString("yyyy-MM-ddHH:mm:ss ") + strMsg, MsgType);
            }
            catch (Exception ex)
            { }
        }

        protected override void OnStart(string[] args)
        {
            rwIni ini = new rwIni(filePath);
            int Interval = ini.ReadInteger("KingleService", "Interval", 1000 * 5);
            WWW_site = ini.ReadString("KingleService", "WWW_Site", "");

            Ticket.Interval = Interval;
            Ticket.Enabled = true;
            Ticket.Elapsed += new System.Timers.ElapsedEventHandler(Ticket_Elapsed);
            Ticket.Start();
        }

        void Ticket_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            Local_Site = ExCore.LocalExternalIP();

            writeLog(Local_Site, EventLogEntryType.Information);
        }

        protected override void OnStop()
        {
            Ticket.Enabled = false;
        }
    }
}

只有注册用户登录后才能发表评论。