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.Tasks;
using System.IO;
using System.Net;
using System.Net.Mail;
namespace WindowsService1
{
public partial class Service1 : ServiceBase
{
System.Timers.Timer timer1; //计时器
int iHour = 00;
int iMinute = 00;
int iSecond = 00;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
timer1 = new System.Timers.Timer();
timer1.Interval = 1000; //设置计时器事件间隔执行时间
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("OnStop", "任务结束");
}
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
int intHour = e.SignalTime.Hour;
int intMinute = e.SignalTime.Minute;
int intSecond = e.SignalTime.Second;
// 设置 每秒钟的开始执行一次
//if (intSecond == iSecond)
//{
// Console.WriteLine("每秒钟的开始执行一次!");
// write(intHour+":"+intMinute+":"+intSecond+" 每30秒钟的开始执行一次!\n\r");
//}
//// 设置 每个小时的30分钟开始执行
//if (intMinute == iMinute && intSecond == iSecond)
//{
// Console.WriteLine("每个小时的30分钟开始执行一次!");
// write("每个小时的30分钟开始执行一次!\n");
//}
// 设置 每天 00:00:00开始执行程序
if (intHour == iHour && intMinute == iMinute && intSecond == iSecond)
{
write(intHour + ":" + intMinute + ":" + intSecond + " 每天00:00:00!\n\r");
}
}
//public void Send()
//{
// //("smtp.126.com", "
[email protected]", "pwd", 25)
// System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
// msg.To.Add("
[email protected]");
// msg.From = new MailAddress("
[email protected]", "pwd", System.Text.Encoding.UTF8);
// msg.Subject = "这是测试邮件";
// msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
// msg.Body = "邮件内容";//邮件内容
// msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
// msg.IsBodyHtml = false;//是否是HTML邮件
// msg.Priority = MailPriority.High;//邮件优先级
// SmtpClient client = new SmtpClient();
// object userState = msg;
// client.Host = "smtp.126.com";
// client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; //该方法即通过外部网络发送.
// client.Credentials = new System.Net.NetworkCredential("username", "pwd");//邮箱登录账号,密码
//}
public void write(string str)
{
string dt = str;
int len = dt.Length;
byte[] inputByts = new byte[len];
inputByts = Encoding.GetEncoding("utf-8").GetBytes(dt);
System.IO.FileStream fs = new System.IO.FileStream("c:/data.txt", System.IO.FileMode.OpenOrCreate);
fs.Seek(0, System.IO.SeekOrigin.End);
fs.Write(inputByts, 0, inputByts.Length);
fs.Close();//关闭流
}
}
}
/////
///// 定时器
/////
//public void TimeDo()
//{
// System.Timers.Timer aTimer = new System.Timers.Timer();
// aTimer.Elapsed += new System.Timers.ElapsedEventHandler(TimeEvent);
// aTimer.Interval = 1000;
// aTimer.Enabled = true;
//}
/////
///// 定时器触发事件
/////
/////
/////
//private static void TimeEvent(object source, System.Timers.ElapsedEventArgs e)
//{
// int intHour = e.SignalTime.Hour;
// int intMinute = e.SignalTime.Minute;
// int intSecond = e.SignalTime.Second;
// int iHour = 10;
// int iMinute = 30;
// int iSecond = 00;
// // 设置 每秒钟的开始执行一次
// if (intSecond == iSecond)
// {
// Console.WriteLine("每秒钟的开始执行一次!");
// }
// // 设置 每个小时的30分钟开始执行
// if (intMinute == iMinute && intSecond == iSecond)
// {
// Console.WriteLine("每个小时的30分钟开始执行一次!");
// }
// // 设置 每天的10:30:00开始执行程序
// if (intHour == iHour && intMinute == iMinute && intSecond == iSecond)
// {
// Console.WriteLine("在每天10点30分开始执行!");
// }
//}