日期:2014-05-18 浏览次数:20956 次
public Service1() { InitializeComponent(); Random rd = new Random(); int intM = rd.Next(02, 03); int intS = rd.Next(00, 59); string str = System.Reflection.Assembly.GetExecutingAssembly().Location; string str1 = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; Console.WriteLine(str); Console.WriteLine(str1); Console.WriteLine(intM); Console.WriteLine(intS); } protected override void OnStart(string[] args) { EventLog.WriteEntry("我的服务启动");//在系统事件查看器里的应用程序事件里来源的描述 writestr("服务启动");//自定义文本日志 System.Timers.Timer t = new System.Timers.Timer(); t.Interval = 1000; t.Elapsed += ChkSrv;//到达时间的时候执行事件; t.AutoReset = true;//设置是执行一次(false)还是一直执行(true); t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件; } /// <summary> /// 定时检查,并执行方法 /// </summary> /// <param name="source"></param> /// <param name="e"></param> public void ChkSrv(object source, System.Timers.ElapsedEventArgs e) { int intHour = e.SignalTime.Hour; int intMinute = e.SignalTime.Minute; int intSecond = e.SignalTime.Second; Random rd = new Random(); int intM = rd.Next(02, 03); int intS = rd.Next(00, 59); if (intHour == 08 && intMinute == intM && intSecond == intS) ///定时设置,判断分时秒 { try { System.Timers.Timer tt = (System.Timers.Timer)source; tt.Enabled = false; SetInnPoint(); tt.Enabled = true; } catch (Exception err) { writestr(err.Message); } } } //我的方法 public static void SetInnPoint() { try { writestr("服务运行"); //这里执行你的东西 Process.Start(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"\KQLoginIn.exe"); Thread.Sleep(10000); } catch (Exception err) { writestr(err.Message); } } ///在指定时间过后执行指定的表达式 /// ///事件之间经过的时间(以毫秒为单位) ///要执行的表达式 public static void SetTimeout(double interval, Action action) { System.Timers.Timer timer = new System.Timers.Timer(interval); timer.Elapsed += (sender, e) => { timer.Enabled = false; action(); }; timer.Enabled = true; } public static void writestr(string readme) { //debug================================================== //StreamWriter dout = new StreamWriter(@"c:/" + System.DateTime.Now.ToString("yyyMMddHHmmss") + ".txt"); using (StreamWriter dout = new StreamWriter(@"c:/" + "WServ_InnPointLog.txt", true)) { dout.Write(String.Format("/r/n事件:{0}/r/n操作时间:{1:yyy-MM-dd HH:mm:ss}", readme, System.DateTime.Now)); //debug================================================== dout.Close(); } } protected override void OnStop() { writestr(&quo