通过.NET平台编写和发布简单的Windows Service
2008-05-07 11:19
357 查看
新建一个(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 (卸载)
如果想实现定时运行,活实时监听的功能,可以用.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 (卸载)
相关文章推荐
- 通过.NET平台编写和发布简单的Windows Service
- 在.net里编写Windows Service
- 艾伟_转载:基于.NET平台的Windows编程实战(四)—— 数据库操作类的编写
- 重温WCF之构建一个简单的WCF(一)(2)通过Windows Service寄宿服务和WCF中实现操作重载
- 基于.NET平台的Windows编程实战(四)—— 数据库操作类的编写
- 基于.NET平台的Windows编程实战(四)—— 数据库操作类的编写
- [WSE]Web Service与Windows Service通过WSE2.0建立订阅/发布关系[更新版本]
- Windows Azure Cloud Service (18) 基于Input Endpoint通过Worker Role发布WCF服务
- 在 C# 平台中, 编写 Windows 服务的简单例子
- [WSE]Web Service与Windows Service通过WSE2.0建立订阅/发布关系[更新版]
- 在.net里编写Windows Service
- 艾伟:基于.NET平台的Windows编程实战(四)—— 数据库操作类的编写
- [WSE]Web Service与Windows Service通过WSE2.0建立订阅/发布关系[更新版本]
- 谈基于.net平台windows开发中的模式窗体
- C# webservice 简单发布
- OpenXLive—Windows Phone上的游戏社交平台发布!
- VS编写 windowsService 服务的注意事项
- C# 编写Windows Service(windows服务程序)【转载】
- 通过service的使用完成简单的音乐播放器