通过.NET平台编写和发布简单的Windows Service是一件很方便的事情.
新建一个(VB.NET/C#)项目,选择Windows Service应用..然后在OnStart类里就可以添加代码了.
如果想实现定时运行,活实时监听的功能,可以用.NET 提供的TIMER类.
以下是代码片段.
private void theTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (!ExCondition())
{ return;
}
try
{
theTimer.Enabled = false;
MyTransaction();
}
catch(Exception es)
{
WriteLog(es.Message);
}
theTimer.Enabled = true;
}
//检测执行条件
private bool ExCondition()
{
DateTime dtNow = DateTime.Now;
if(this.currentmode.Equals("auto"))
{
string[] frev = this.freqvalue.Split('-');
switch( this.freq)
{
case "2"://2-每小时
if( (dtNow.Minute == 59)&(dtNow.Second< 30))
{
return true;
}
break;
case "4"://4-每天,
if((dtNow.Hour == int.Parse(frev[1]))&(dtNow.Minute == int.Parse(frev[2]))&(dtNow.Second<30))
{
return true;
}
break;
case "8"://8-每周,
int we = int.Parse(frev[0].Substring(1,1));
if((dtNow.DayOfWeek.ToString().Equals(Week[we]))&(dtNow.Hour == int.Parse(frev[1]))&(dtNow.Minute == int.Parse(frev[2]))&(dtNow.Second<30))
{
return true;
}
break;
case "16"://16-每月,
int mm = int.Parse(frev[0].Substring(1,frev[0].Length-1));
if((dtNow.Day == mm)&(dtNow.Hour == int.Parse(frev[1]))&(dtNow.Minute == int.Parse(frev[2]))&(dtNow.Second<30))
{
return true;
}
break;
default://无触发
return true;
break;
}
}
else
{
switch(this.startpoint)
{
case "0":
return true;
break;
default:
string[] st = startpoint.Split('-');
if((dtNow.Hour == int.Parse(st[0]))&(dtNow.Minute ==int.Parse(st[1]))&(dtNow.Second<30))
{
return true;
}
break;
}
}
return false;
}
注册你的Windows Service程序.
用InstallUtil IASserver.exe (这个文件默认在C:\WINNT\Microsoft.NET\Framework\v1.1.4322下)
InstallUtil IASserver.exe
InstallUtil IASserver.exe /u (卸载)
本文地址:http://com.8s8s.com/it/it43059.htm