赞
踩
可以在原有项目中新建项目
选中Visual c#-》Windows->Windows 服务-》确定
具体操作见使用c#创建Windows服务,将第二部分操作完成即可。
右击service类查看代码,添加你需要的服务代码
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.IO; using CarpaServer.Vch; using Carpa.Web.Script; using CarpaServer; using CarpaServer.Service; using System.Configuration; namespace MyWindowsService { public partial class Service1 : ServiceBase { public Service1() { InitializeComponent(); } public static bool timerIsRuning = false; string filePath = @"D:\MyServiceLog\"; BillIndexAuditCxService dbcs = new BillIndexAuditCxService(); /// <summary> /// 启动 /// </summary> /// <param name="args"></param> /// 如果要调试服务程序,将OnStart方法设置成public的无参方法即可 //public void OnStart() protected override void OnStart(string[] args) { string timeStr = DateTime.Now.ToString("yyyy-MM-dd"); filePath += "MyServiceLog" + timeStr + ".txt"; using (FileStream stream = new FileStream(filePath,FileMode.Append)) using (StreamWriter writer = new StreamWriter(stream)) { writer.WriteLine("---------------------" + DateTime.Now + ",服务启动!" + "---------------------"); } System.Timers.Timer timer = new System.Timers.Timer(10000*6*30);//创建定时器10s执行一次 10s*6*30=30min执行一次 timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); timer.AutoReset = true;//设置是一次执行还是一直执行 timer.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件 } public void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { if (timerIsRuning) return; timerIsRuning = true; using (FileStream stream = new FileStream(filePath, FileMode.Append)) using (StreamWriter writer = new StreamWriter(stream)) { try { var list = dbcs.PullBill();//这里是调用的我写好的操作方法,改成自己需要的代码即可 string txt = DateTime.Now + ",更新结果:" + list; writer.WriteLine(txt); } catch (Exception ex) { writer.WriteLine("-------" + DateTime.Now + ":获取单据失败," + ex.StackTrace + "-------"); throw; } timerIsRuning = false; } } protected override void OnStop() { using (FileStream stream = new FileStream(filePath, FileMode.Append)) using (StreamWriter writer = new StreamWriter(stream)) { writer.WriteLine("---------------------" + DateTime.Now + ",服务停止!" + "---------------------"); } } } }
若需要调试,除了将上述注释代码复原外,还需要修改program类,注释掉原有代码,打开调试用代码
using System; using System.Collections.Generic; using System.Linq; using System.ServiceProcess; using System.Text; namespace MyWindowsService { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> static void Main() { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new Service1() }; ServiceBase.Run(ServicesToRun); //调试用代码 //Service1 s1 = new Service1(); //s1.OnStart(); } } }
写完代码后需要在计算机上创建服务
1、WIN+R打开运行输入cmd
2.、输入 sc create ServerName binpath= “E:\code\bin\MyWindowsService.exe” 等号后有空格,其中E:\code\bin\MyWindowsService.exe为你的服务生成dll的地址
sc create ServerName binpath= "E:\code\bin\MyWindowsService.exe"
每次修改代码后,需要先停用服务,重新生成代码,再打开服务。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。