在.NET环境写编写和注册Windws Service
2024-07-10 13:01:39
供稿:网友
国内最大的酷站演示中心!
/*====================================================================
虽然简单,但是我们很少写基于这样的应用,我偶尔看到这篇文章,就顺手摘录下来了
通过.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 (卸载)