window服务: 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; namespace WindowsService2 { public partial class Service1 : ServiceBase { System.Timers.Timer timer1; //计时器 WindowTaske w = new WindowTaske(); public Service1() { InitializeComponent(); } protected override void OnStart(string[] args) { timer1 = new System.Timers.Timer(); timer1.Interval = 3000; //设置计时器事件间隔执行时间 timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed); timer1.Enabled = true; if (!EventLog.SourceExists("OnStart222")) { EventLog.CreateEventSource("OnStart222", "jason"); } EventLog.WriteEntry("OnStart222", "开始任务了"); } protected override void OnStop() { this.timer1.Enabled = false; EventLog.WriteEntry("OnStart222", "任务结束"); } private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { if (e.SignalTime.Hour == 0 && e.SignalTime.Minute == 0 && e.SignalTime.Second == 0)//晚上12点发送邮件 { //发送邮件方法 } } } }
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; namespace WindowsService2 { public partial class Service1 : ServiceBase { System.Timers.Timer timer1; //计时器 WindowTaske w = new WindowTaske(); public Service1() { InitializeComponent(); } protected override void OnStart(string[] args) { timer1 = new System.Timers.Timer(); timer1.Interval = 3000; //设置计时器事件间隔执行时间 timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed); timer1.Enabled = true; if (!EventLog.SourceExists("OnStart222")) { EventLog.CreateEventSource("OnStart222", "jason"); } EventLog.WriteEntry("OnStart222", "开始任务了"); } protected override void OnStop() { this.timer1.Enabled = false; EventLog.WriteEntry("OnStart222", "任务结束"); } private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { if (e.SignalTime.Hour == 0 && e.SignalTime.Minute == 0 && e.SignalTime.Second == 0)//晚上12点发送邮件 { //发送邮件方法 } } } }
... 如果人家是虚拟机 你怎么写服务? 连个远程都没有 只有空间呢? 所以 我只想问 为什么没有人用SQL作业呢? 新建一个作业 晚上执行存储过程 你用存储过程请求webservice也好 请求ashx也罢 甚至 你可以本身就用sql发送,,这么多的选择 而且 效率比所谓的timer高了很多吧.. 为什么没人选用呢?
只是为了12点发封邮件用Windows Service真心觉得太浪费了,而且用Timer并不能保证一定能发出去,判断条件是小时分钟和秒都为0,这对你的启动时间有要求, 因为你的间隔是3秒,假设你23:59:59秒启动,就发不出去了。所以个人觉得,应该用任务计划,或者SQL Server的JOB。关于用SQL server发邮件请自行百度sp_send_dbmail [quote=引用 21 楼 chz415767975 的回复:] [quote=引用 7 楼 zhanglong_longlong 的回复:] window服务: 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; namespace WindowsService2 { public partial class Service1 : ServiceBase { System.Timers.Timer timer1; //计时器 WindowTaske w = new WindowTaske(); public Service1() { InitializeComponent(); } protected override void OnStart(string[] args) { timer1 = new System.Timers.Timer(); timer1.Interval = 3000; //设置计时器事件间隔执行时间 timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed); timer1.Enabled = true; if (!EventLog.SourceExists("OnStart222")) { EventLog.CreateEventSource("OnStart222", "jason"); } EventLog.WriteEntry("OnStart222", "开始任务了"); } protected override void OnStop() { this.timer1.Enabled = false; EventLog.WriteEntry("OnStart222", "任务结束"); } private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { if (e.SignalTime.Hour == 0 && e.SignalTime.Minute == 0 && e.SignalTime.Second == 0)//晚上12点发送邮件 { //发送邮件方法 } } } }
[quote=引用 7 楼 zhanglong_longlong 的回复:] window服务: 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; namespace WindowsService2 { public partial class Service1 : ServiceBase { System.Timers.Timer timer1; //计时器 WindowTaske w = new WindowTaske(); public Service1() { InitializeComponent(); } protected override void OnStart(string[] args) { timer1 = new System.Timers.Timer(); timer1.Interval = 3000; //设置计时器事件间隔执行时间 timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed); timer1.Enabled = true; if (!EventLog.SourceExists("OnStart222")) { EventLog.CreateEventSource("OnStart222", "jason"); } EventLog.WriteEntry("OnStart222", "开始任务了"); } protected override void OnStop() { this.timer1.Enabled = false; EventLog.WriteEntry("OnStart222", "任务结束"); } private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { if (e.SignalTime.Hour == 0 && e.SignalTime.Minute == 0 && e.SignalTime.Second == 0)//晚上12点发送邮件 { //发送邮件方法 } } } }
62,041
社区成员
669,051
社区内容
加载中
.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。
希望和大家一起共同营造一个活跃、友好的社区氛围。
试试用AI创作助手写篇文章吧