服务定时器

本文详细介绍了如何在Windows服务中实现定时执行HTTP请求的功能,包括新建服务、配置服务名称、编写代码使用System.Timers库定时调用指定URL,以及在Designer中设置服务启动类型为自动。

第一步新建服务(Windows服务)

第二步右键属性修改ServiceName

第三步编写代码

System.Timers.Timer task;

protected override void OnStart(string[] args)
{
  task=new System.Timers.Timer();
  task.Interval=1800000;//10分钟一次
  task.Elapsed+=new System.Timers.ElapserEventHandler(ExpressOrder);//到时间执行事件
  task.AutoReset=true;//设置执行一次(false),一直执行(true)
  task.Enabled=true;//是否执行System.Timers.Timer.Elapsed事件  
}

public void ExpressOrder(object source,System.Timers.ElapsedEventArgs e)
{
  try{
      string url=ConfigurationManager.AppSettings["appconfig中定义的url地址"];
      System.Net.WebClient client=new System.Net.WebClient();
      byte[] bytes={};
      client.Upload(url,"POST",bytes);  
  }catch(Exception ex){throw new Exception(ex.Message);}  
}

第四步在Designer中新增一条本地执行的语句(无需密码、账户等)

(在serviceInstaller1下)this.serviceInstaller1.StartType=System.ServiceProcess.ServiceStartMode.Automatic;

 

转载于:https://www.cnblogs.com/xibianriluo/p/5257165.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值