windows服务的调试
1 using System; 2 using System.Collections.Generic; 3 using System.ServiceProcess; 4 using System.Text; 5 6 namespace WSFileTransfer 7 { 8 static class Program 9 { 10 /// <summary> 11 /// 应用程序的主入口点。 12 /// </summary> 13 static void Main() 14 { 15 ServiceBase[] ServicesToRun; 16 ServicesToRun = new ServiceBase[] 17 { 18 new FileTransferService() 19 }; 20 ServiceBase.Run(ServicesToRun); 21 22 //new FileTransferService().DataSynch(null); 23 } 24 } 25 }
这个Program.cs文件,也就是windows服务的入口,而FiletTransferService类的实现
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Diagnostics; 6 using System.ServiceProcess; 7 using System.Text; 8 using Dtsc.Common.FileTransfer; 9 using System.Threading; 10 11 namespace WSFileTransfer 12 { 13 public partial class FileTransferService : ServiceBase 14 { 15 Timer timer; 16 int days = ConfigReader.GetAppSettingInt("SleepDays"); 17 int hours = ConfigReader.GetAppSettingInt("SleepHours"); 18 int minutes = ConfigReader.GetAppSettingInt("SleepMinutes"); 19 int seconds = ConfigReader.GetAppSettingInt("SleepSeconds"); 20 21 public FileTransferService() 22 { 23 InitializeComponent(); 24 } 25 26 protected override void OnStart(string[] args) 27 { 28 try 29 { 30 if (days + hours + minutes + seconds == 0) 31 { 32 hours = 1; 33 } 34 timer = new System.Threading.Timer(new TimerCallback(DataSynch), null, new TimeSpan(1000), new TimeSpan(days, hours, minutes, seconds)); 35 WriteLog.WriteInformation("服务成功启动!"); 36 } 37 catch (Exception exc) 38 { 39 WriteLog.WriteException(exc); 40 throw; 41 } 42 } 43 44 protected override void OnStop( ) 45 { 46 try 47 { 48 timer.Dispose(); 49 WriteLog.WriteInformation("服务成功终止!"); 50 } 51 catch (Exception exc) 52 { 53 WriteLog.WriteException(exc); 54 } 55 } 56 57 /// <summary> 58 /// 运行方法 59 /// </summary> 60 /// <param name="sender">The sender.</param> 61 public void DataSynch(object sender) 62 { 63 StringBuilder sbDebugInfo = new StringBuilder(); 64 try 65 { 66 TransferHelper.CheckServerList(); 67 } 68 catch (Exception exc) 69 70 { 71 WriteLog.WriteException(exc); 72 } 73 finally 74 { 75 SleepThread(); 76 WriteLog.WriteInformation(sbDebugInfo.ToString()); 77 } 78 } 79 80 /// <summary> 81 /// 线程休眠 82 /// </summary> 83 /// <param name="client">客户端代理</param> 84 private void SleepThread() 85 { 86 timer.Change(new TimeSpan(days, hours, minutes, seconds), new TimeSpan(days, hours, minutes, seconds)); 87 } 88 } 89 }
在网上大家都说附加进程进行调试,其实还有一个方法
using System; using System.Collections.Generic; using System.ServiceProcess; using System.Text; namespace WSFileTransfer { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> static void Main() { /*ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new FileTransferService() }; ServiceBase.Run(ServicesToRun); */
new FileTransferService().DataSynch(null); } } }
就OK拉