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{}
}
}
}
}
Windows服务代码练习
最新推荐文章于 2023-12-27 10:44:53 发布