利用VC#.NET 2005 的Windows服务项目创建一个Windows服务
2011-01-07 16:02
756 查看
利用VC#.NET 2005 的Windows服务项目创建一个Windows服务
利用VC#.NET 2005 的Windows服务项目创建一个Windows服务。功能:每一小时提示一个对话框!
#主程式 Program.cs
using System.Collections.Generic;
using System.ServiceProcess;
using System.Text;
using System;
namespace TimeForAction
{
static class Program
{
//将有SCM System Control Manager来调用
static void Main(string[] args) {
ServiceBase.Run(new TimeForAction());
}
}
}
//TimeForAction.cs 实现类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace TimeForAction
{
public partial class TimeForAction : ServiceBase
{
private Thread MyThread;
public TimeForAction()
{
InitializeComponent();
MyThread = new Thread(new ThreadStart(ThreadFunc));
MyThread.Priority = ThreadPriority.Lowest;
}
protected override void OnStart(string[] args) //实现ServiceBase的接口
{
MyThread.Start();
}
protected override void OnStop() //实现ServiceBase的接口
{
MyThread.Abort();
}
private void ThreadFunc() //线程入口函数
{
int LastHour = DateTime.Now.Hour;
while (true)
{
System.Threading.Thread.Sleep(60000);
if (DateTime.Now.Hour - 1 == LastHour)
{
MessageBox.Show("为了您的身体,请起来动动休息5分钟!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
LastHour = DateTime.Now.Hour;
}
}
}
}
}
//TimeForAction.Designer.cs //界面设计
TimeForActionnamespace TimeForAction
{
partial class TimeForAction
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "TimeForAction";
}
}
}
最后编译并生成。不能直接运行。必须先安装为windows 服务。 在设计页面上点右键,出现菜单后,选择添加安装程序。这时会出现一个新的页面,页面上有控件 serviceProcessInstaller1和serviceInstaller1 ,在 serviceProcessInstaller1中把属性Account改为LocalSystem,在把serviceInstaller1中把属性Parent 改为serviceProcessInstaller1 完成之后编译并生成,假设生成cjb.exe 那么进入WINDOWS\Microsoft.NET\Framework\v2.0.50727
InstallUtil cjb.exe 安装服务
InstallUtil cjb.exe -u 卸载服务
最后就可以看到系统服务选项就有你安装的服务了。
利用VC#.NET 2005 的Windows服务项目创建一个Windows服务。功能:每一小时提示一个对话框!
#主程式 Program.cs
using System.Collections.Generic;
using System.ServiceProcess;
using System.Text;
using System;
namespace TimeForAction
{
static class Program
{
//将有SCM System Control Manager来调用
static void Main(string[] args) {
ServiceBase.Run(new TimeForAction());
}
}
}
//TimeForAction.cs 实现类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace TimeForAction
{
public partial class TimeForAction : ServiceBase
{
private Thread MyThread;
public TimeForAction()
{
InitializeComponent();
MyThread = new Thread(new ThreadStart(ThreadFunc));
MyThread.Priority = ThreadPriority.Lowest;
}
protected override void OnStart(string[] args) //实现ServiceBase的接口
{
MyThread.Start();
}
protected override void OnStop() //实现ServiceBase的接口
{
MyThread.Abort();
}
private void ThreadFunc() //线程入口函数
{
int LastHour = DateTime.Now.Hour;
while (true)
{
System.Threading.Thread.Sleep(60000);
if (DateTime.Now.Hour - 1 == LastHour)
{
MessageBox.Show("为了您的身体,请起来动动休息5分钟!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
LastHour = DateTime.Now.Hour;
}
}
}
}
}
//TimeForAction.Designer.cs //界面设计
TimeForActionnamespace TimeForAction
{
partial class TimeForAction
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "TimeForAction";
}
}
}
最后编译并生成。不能直接运行。必须先安装为windows 服务。 在设计页面上点右键,出现菜单后,选择添加安装程序。这时会出现一个新的页面,页面上有控件 serviceProcessInstaller1和serviceInstaller1 ,在 serviceProcessInstaller1中把属性Account改为LocalSystem,在把serviceInstaller1中把属性Parent 改为serviceProcessInstaller1 完成之后编译并生成,假设生成cjb.exe 那么进入WINDOWS\Microsoft.NET\Framework\v2.0.50727
InstallUtil cjb.exe 安装服务
InstallUtil cjb.exe -u 卸载服务
最后就可以看到系统服务选项就有你安装的服务了。
相关文章推荐
- 如何在 VisualC # .NET 和 VisualC # 2005 中创建安装项目用于 Windows 服务应用程序
- vs2010 创建windows服务( Asp.net(C#) windows 服务 用于实现计划任务,事件监控等)
- 使用普通Windows服务创建Quartz.Net服务项目
- vs2010 创建windows服务( Asp.net(C#) windows 服务 用于实现计划任务,事件监控等)
- C# 利用 Windows服务模板 创建、安装与卸载Windows服务
- 【翻译】在Visual C#中如何创建一个Windows服务的安装项目
- 如何创建一个 Windows 服务应用程序的安装项目在 Visual C# 中
- Quartz.NET 2.0 学习笔记(5) :实例创建Windows服务实现任务调度 Quartz.NET 项目地址 http://quartznet.sourceforge.net/ Quar
- 在VC++中创建的项目中添加一个资源文件
- 如何把一个lib库文件加入到VC.NET项目中去
- [Solution] Microsoft Windows 服务(2) 使用Topshelf创建Windows服务
- C#创建Windows服务(Windows Services) 实战之系统定时重启服务
- 在ASP.NET3.5下利用Linq,Ajax创建一个线上网络聊天室
- 一个完整的Windows 服务从创建到安装卸载
- .net Windows服务程序和安装程序制作图解 及 VS 2010创建、安装、调试 windows服务(windows service)
- 方便地启动Oracle服务(VB.NET 2005 Windows服务操控与多线程)
- Quartz.NET 2.0 学习笔记(5) :实例创建Windows服务实现任务调度
- vs(2005 and 2008)中使用vc++创建智能设备项目失败的正确解决方案
- Tips:vc2005无法新建智能项目创建的解决方法
- c# sqlite 数据库加密2010-05-29 10:55用了ADO.NET 2.0 SQLite Data Provider 这样可以直接利用它来创建一个加密的sqlite数据库。