Windows服务代码练习

来源:互联网 发布:数据新常态 mobi 编辑:程序博客网 时间:2024/04/30 21:07

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Diagnostics;using System.Linq;using System.ServiceProcess;using System.Text;using System.Threading;namespace WindowsServiceTest{    public partial class ServiceTest : ServiceBase    {        public ServiceTest()        {            InitializeComponent();        }        protected override void OnStart(string[] args) //启动服务的时候写个日志        {            proccess.Start();                }        protected override void OnStop() //关闭服务的时候再写个日志        {            using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true))            {                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "Stop.");            }        }        public static class proccess        {            public static void Start()            {               ThreadStart start = new ThreadStart(ThreadAction);               Thread th = new Thread(start);               th.IsBackground = true;               th.Start();            }        }        public static void ThreadAction()        {            while(true)            {                 try                {                    //do something                    System.Threading.Thread.Sleep(30000);                }                catch{}            }        }    }}


0 0
原创粉丝点击