【编者按】:网学网ASP.net为您提供在WebService中使用Timer的方法参考,解决您在在WebService中使用Timer的方法学习中工作中的难题,参考学习。
在WebService开发时,有时需要做轮询操作,可以使用Timer ,注意是System.Timers.Timer,且事件的触发必须是经过一次WebService调用后。 using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
...{
public Service () ...{
//如果使用设计的组件,请取消注释以下行
InitializeComponent();
}
private System.Timers.Timer timer1;
private System.ComponentModel.IContainer components;
private void InitializeComponent()
...{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Timers.Timer();
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 1000;
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);
}
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
...{
using (StreamWriter sw = new StreamWriter(Server.MapPath(".")+"TextFile.txt"))
...{
sw.WriteLine(DateTime.Now);
}
}