C#通过Windows服务(Windows Service)和System.Timers.Timer做定时任务功能

学习Windows服务制作;http://blog.csdn.net/u011966339/article/details/79549293

修改Service1中的代码:如下

打开Service1 写入想要执行的操作等:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public  partial  class  Service1 : ServiceBase
     {
         System.Timers.Timer timer1;   //计时器
         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 ;
 
             
 
         }
         ///
         /// 定时器 调用的方法
         ///
         ///
         ///
         private  void  timer1_Elapsed( object  sender, System.Timers.ElapsedEventArgs e)
         {
             var  client =  new  HttpClient();
             client.BaseAddress =  new  Uri( "http://192.168.10.239:9000/" );//接口url
             string  data = client.GetStringAsync( "ZyTest" ).Result; //接口action
         }
 
         protected  override  void  OnStop()
         {
             //服务结束执行代码
             this .timer1.Enabled =  false ;
         }
 
 
         protected  override  void  OnPause()
         {
             //服务暂停执行代码
             base .OnPause();
         }
         protected  override  void  OnContinue()
         {
             //服务恢复执行代码
             base .OnContinue();
         }
         protected  override  void  OnShutdown()
         {
             //系统即将关闭执行代码
             base .OnShutdown();
         }
     }

你可能感兴趣的:(ASP.net网站知识)