.net Service定时启动
来源:互联网 发布:淘宝基础版 全屏店招 编辑:程序博客网 时间:2024/06/05 08:33
2.Service1添加安装程序
设计->右键->添加安装程序;
3.ProjectInstaller设置属性
serviceInstaller1
Modifiers:public;
ServiceName:Service1(你的服务名称);
StartType:Automatic;
serviceProcessInstaller1
Account:LocalSystem;(默认User的情况下Start.bat启动后需要输入账号密码)
4.Service1代码
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Diagnostics;using System.Linq;using System.ServiceProcess;using System.Text;using System.Timers;namespace Service1{ public partial class Service1 : ServiceBase { #region 属性 private System.Timers.Timer timer; private BackgroundWorker worker; #endregion public Service1() { InitializeComponent(); } /// 启动服务 /// <summary> /// 启动服务 /// </summary> /// <param name="args"></param> protected override void OnStart(string[] args) { Init(); } /// 初始化服务 /// <summary> /// 初始化服务 /// </summary> private void Init() { worker = new BackgroundWorker(); worker.DoWork+=new DoWorkEventHandler(worker_DoWork); timer = new System.Timers.Timer(); timer.Interval = 60 * 1000;//1分钟执行一次//1000*60*60 1小时//1000*60*60*24 1天 timer.Elapsed+=new ElapsedEventHandler(timer_Elapsed); timer.Enabled = false; timer.Start(); } /// 定时执行 /// <summary> /// 定时执行 /// </summary> /// <param name="sender"></param> /// <param name="args"></param> void timer_Elapsed(object sender,ElapsedEventArgs args) { if (!worker.IsBusy) { worker.RunWorkerAsync(); } } /// 后台执行进程 /// <summary> /// 后台执行进程 /// </summary> /// <param name="sender"></param> /// <param name="args"></param> void worker_DoWork(object sender, DoWorkEventArgs args) { try { MainFunction(); } catch (Exception ex) { } } private void MainFunction() { //主要代码 } /// 停止服务 /// <summary> /// 停止服务 /// </summary> protected override void OnStop() { timer.Stop(); } }}
5.配置文件
在项目中右键->添加新建项目->应用程序配置文件,默认名称App.config;
在文件中添加程序所需的框架,<ConnectionStrings>中添加数据库连接,<appSettings>中添加服务器IP;
(如果程序涉及调用SAP接口,需要在<appSettings>中添加destination)
6.调试
在Debug文件夹中启动start.bat,然后在vs中工具->附加到进程找到服务名,添加后程序会执行到断点处,就可以调试了;
注意服务名是Service.exe,不是Service.vshost.exe。如果没有找到服务名,勾选显示所有用户的进程,就可以找到了;
(如果需要部署到服务器,只需要将Debug文件夹拷到相应的文件夹下,启动start.bat即可)
此外放上start.bat和stop.bat的代码:
start.bat
@ECHO off
cls
color 0A
ECHO 安装APP SERVER...
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.EXE Service1.exe
net start Service1
pause
EXIT
stop.bat
@ECHO off
cls
color 0A
ECHO 安装APP SERVER...
net stop Service1
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.EXE /u Service1.exe
pause
EXIT
- .net Service定时启动
- 启动 ASP.NET State service
- 使用ASP.NET实现Windows Service定时执行任务
- 使用ASP.NET实现Windows Service定时执行任务
- 使用ASP.NET实现Windows Service定时执行任务
- 使用ASP.NET实现Windows Service定时执行任务
- 在Listener(监听器)定时启动的TimerTask(定时任务)中使用Spring@Service注解的bean
- Service+AlarmManager 定时任务
- (原创)定时启动某个activity或者service的实现方式
- 项目启动后开启定时任务方法-->TimerTask中如何调用service
- Quartz-Spring定时任务器持久化,通过Service动态添加,删除,启动暂停任务
- 报没有启动 ASP.NET State service错误的解决方法
- 没有启动 ASP.NET State service错误的解决方法
- Net.Tcp Port Sharing Service服务无法启动
- 没有启动 ASP.NET State service错误的解决方法
- 启动asp.net state service服务提示1058
- 启动Service
- 请确保 ASP.NET State Service (ASP.NET 状态服务)已启动解决办法
- tomcat 启动找不到.class文件
- 华硕笔记本出厂系统替换其他系统
- OTT系统和IPTV方案哪个更适合用于搭建局域网视频点播直播
- 记录三个问题
- HDU 2006
- .net Service定时启动
- 远程桌面不能复制粘贴解决办法
- js禁用回退键[backspace键]浏览历史跳转的解决办法
- iptables的基本参数和简单使用方法
- XP如何连接带网口的共享打印机
- Java 提供接口的安全问题
- RequestDispatcher分派请求
- linux(9)
- 获取手机联系人的两种方法