/// 应用程序的主入口点。        ///         static void Main()        {            ServiceBase[] ServicesToRun;            ServicesToRun = new ServiceBase[]             {                 new Service1()             };            ServiceBase.Run(ServicesToRun);        }    }}  3.在新建的工程中,点击Service1.cs文件,切换到代码视图,生成的代码继承于ServiceBase基类,并重载了OnStart和OnStop方法。我在这个文件中进行了一些简单的操作,就是在服务开始的时候,定义一个定时器,然后每隔1秒钟,向文件中写入当前时间。 代码 namespace WindowsService1{    public partial class Service1 : ServiceBase    {        Timer timer;        public Service1()        {            InitializeComponent();        }        protected override void OnStart(string[] args)        {            timer = new Timer(1000);            timer.Elapsed = new ElapsedEventHandler(timer_Elapsed);            timer.Start();        }        protected override void OnStop()        {            timer.Stop();            timer.Dispose();        }        void timer_Elapsed(object sender, ElapsedEventArgs e)        {            string filePath = AppDomain.CurrentDomain.BaseDirectory  "test.txt";            StreamWriter sw = null;            if (!File.Exists(filePath))            {                sw = File.CreateText(filePath);            }            else            {                sw = File.AppendText(filePath);            }            sw.Write("访问时间:"DateTime.Now.ToString()Environment.NewLine);            sw.Close();        }    }}4.向工程中添加一个安装程序类。 4.在新添加的安装程序类中,设定服务的名称,启动方式,账号名和密码等信息。 代码 namespace WindowsService1{    partial class Installer1    {        ///         /// 必需的设计器变量。        ///         private System.ComponentModel.IContainer components = null;        private System.ServiceProcess.ServiceProcessInstaller spInstaller;        private System.ServiceProcess.ServiceInstaller sInstaller;        ///          /// 清理所有正在使用的资源。        ///         /// 如果应释放托管资源,为 true;否则为 false。        protected override void Dispose(bool disposing)        {            if (disposing && (components != null))            {                components.Dispose();            }            base.Dispose(disposing);        }        #region 组件设计器生成的代码        ///         /// 设计器支持所需的方法 - 不要        /// 使用代码编辑器修改此方法的内容。        ///         private void InitializeComponent()        {            components = new System.ComponentModel.Container();            // 创建ServiceProcessInstaller对象和ServiceInstaller对象            this.spInstaller =new System.ServiceProcess.ServiceProcessInstaller();            this.sInstaller = new System.ServiceProcess.ServiceInstaller();            // 设定ServiceProcessInstaller对象的帐号、用户名和密码等信息            this.spInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;            this.spInstaller.Password = null;            this.spInstaller.Username = null;            // 设定服务的名称            this.sInstaller.ServiceName = "WindowsService1";            //设定服务启动的方式            this.sInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;            this.Installers.AddRange(new System.Configuration.Install.Installer[] {            this.spInstaller,this.sInstaller});        }        #endregion    }}5.生成工程,在bin目录下会生成exe文件。如果直接运行exe文件的话,是不能执行的,需要使用安装Windows服务用到一个名为InstallUtil.exe的命令行工具,打开命令行工具,转到InstallUtil.exe的目录下,我安装的是VS 2010,对应的目录为:C:WindowsMicrosoft.NETFrameworkv4.0.30319InstallUtil.exe,然后执行InstallUtil.exe待执行的exe文件的目录,如:InstallUtil.exe F:MyProjectWindowsService1WindowsService1inDebugWindowsService1.exe。执行成功后,会在Windows的服务中,出现了刚刚添加的服务的名称。 6.启动该服务,这时打开binDebug文件夹,发现已经生成了一个test.txt的文件,里面记录了时间。这说明服务已经正式开始执行。7.停止服务的操作也和简单,打开命令行工具,转到C:WindowsMicrosoft.NETFrameworkv4.0.30319目录,然后执行InstallUtil.exe - u F:MyProjectWindowsService1WindowsService1inDebugWindowsService1.exe命令就可以了。-IMDN开发者社群-imdn.cn">         /// 应用程序的主入口点。        ///         static void Main()        {            ServiceBase[] ServicesToRun;            ServicesToRun = new ServiceBase[]             {                 new Service1()             };            ServiceBase.Run(ServicesToRun);        }    }}  3.在新建的工程中,点击Service1.cs文件,切换到代码视图,生成的代码继承于ServiceBase基类,并重载了OnStart和OnStop方法。我在这个文件中进行了一些简单的操作,就是在服务开始的时候,定义一个定时器,然后每隔1秒钟,向文件中写入当前时间。 代码 namespace WindowsService1{    public partial class Service1 : ServiceBase    {        Timer timer;        public Service1()        {            InitializeComponent();        }        protected override void OnStart(string[] args)        {            timer = new Timer(1000);            timer.Elapsed = new ElapsedEventHandler(timer_Elapsed);            timer.Start();        }        protected override void OnStop()        {            timer.Stop();            timer.Dispose();        }        void timer_Elapsed(object sender, ElapsedEventArgs e)        {            string filePath = AppDomain.CurrentDomain.BaseDirectory  "test.txt";            StreamWriter sw = null;            if (!File.Exists(filePath))            {                sw = File.CreateText(filePath);            }            else            {                sw = File.AppendText(filePath);            }            sw.Write("访问时间:"DateTime.Now.ToString()Environment.NewLine);            sw.Close();        }    }}4.向工程中添加一个安装程序类。 4.在新添加的安装程序类中,设定服务的名称,启动方式,账号名和密码等信息。 代码 namespace WindowsService1{    partial class Installer1    {        ///         /// 必需的设计器变量。        ///         private System.ComponentModel.IContainer components = null;        private System.ServiceProcess.ServiceProcessInstaller spInstaller;        private System.ServiceProcess.ServiceInstaller sInstaller;        ///          /// 清理所有正在使用的资源。        ///         /// 如果应释放托管资源,为 true;否则为 false。        protected override void Dispose(bool disposing)        {            if (disposing && (components != null))            {                components.Dispose();            }            base.Dispose(disposing);        }        #region 组件设计器生成的代码        ///         /// 设计器支持所需的方法 - 不要        /// 使用代码编辑器修改此方法的内容。        ///         private void InitializeComponent()        {            components = new System.ComponentModel.Container();            // 创建ServiceProcessInstaller对象和ServiceInstaller对象            this.spInstaller =new System.ServiceProcess.ServiceProcessInstaller();            this.sInstaller = new System.ServiceProcess.ServiceInstaller();            // 设定ServiceProcessInstaller对象的帐号、用户名和密码等信息            this.spInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;            this.spInstaller.Password = null;            this.spInstaller.Username = null;            // 设定服务的名称            this.sInstaller.ServiceName = "WindowsService1";            //设定服务启动的方式            this.sInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;            this.Installers.AddRange(new System.Configuration.Install.Installer[] {            this.spInstaller,this.sInstaller});        }        #endregion    }}5.生成工程,在bin目录下会生成exe文件。如果直接运行exe文件的话,是不能执行的,需要使用安装Windows服务用到一个名为InstallUtil.exe的命令行工具,打开命令行工具,转到InstallUtil.exe的目录下,我安装的是VS 2010,对应的目录为:C:WindowsMicrosoft.NETFrameworkv4.0.30319InstallUtil.exe,然后执行InstallUtil.exe待执行的exe文件的目录,如:InstallUtil.exe F:MyProjectWindowsService1WindowsService1inDebugWindowsService1.exe。执行成功后,会在Windows的服务中,出现了刚刚添加的服务的名称。 6.启动该服务,这时打开binDebug文件夹,发现已经生成了一个test.txt的文件,里面记录了时间。这说明服务已经正式开始执行。7.停止服务的操作也和简单,打开命令行工具,转到C:WindowsMicrosoft.NETFrameworkv4.0.30319目录,然后执行InstallUtil.exe - u F:MyProjectWindowsService1WindowsService1inDebugWindowsService1.exe命令就可以了。 - IMDN开发者社群-imdn.cn">
登录
首页 » C# » C# 制作系统服务 实例源码下载

C# 制作系统服务 实例源码下载

于 2015-04-21 发布
0 258
下载积分: 1 下载次数: 0

代码说明:

附件中有详细的安装使用文档,大概步骤如下:1.新建Windows项目,选择"Windows服务"类型的工程。2.生成的Program.cs文件中,定义了服务启动的Main函数。 代码 namespace WindowsService1{    static class Program    {        ///         /// 应用程序的主入口点。        ///         static void Main()        {            ServiceBase[] ServicesToRun;            ServicesToRun = new ServiceBase[]             {                 new Service1()             };            ServiceBase.Run(ServicesToRun);        }    }}  3.在新建的工程中,点击Service1.cs文件,切换到代码视图,生成的代码继承于ServiceBase基类,并重载了OnStart和OnStop方法。我在这个文件中进行了一些简单的操作,就是在服务开始的时候,定义一个定时器,然后每隔1秒钟,向文件中写入当前时间。 代码 namespace WindowsService1{    public partial class Service1 : ServiceBase    {        Timer timer;        public Service1()        {            InitializeComponent();        }        protected override void OnStart(string[] args)        {            timer = new Timer(1000);            timer.Elapsed  = new ElapsedEventHandler(timer_Elapsed);            timer.Start();        }        protected override void OnStop()        {            timer.Stop();            timer.Dispose();        }        void timer_Elapsed(object sender, ElapsedEventArgs e)        {            string filePath = AppDomain.CurrentDomain.BaseDirectory   "test.txt";            StreamWriter sw = null;            if (!File.Exists(filePath))            {                sw = File.CreateText(filePath);            }            else            {                sw = File.AppendText(filePath);            }            sw.Write("访问时间:" DateTime.Now.ToString() Environment.NewLine);            sw.Close();        }    }}4.向工程中添加一个安装程序类。 4.在新添加的安装程序类中,设定服务的名称,启动方式,账号名和密码等信息。 代码 namespace WindowsService1{    partial class Installer1    {        ///         /// 必需的设计器变量。        ///         private System.ComponentModel.IContainer components = null;        private System.ServiceProcess.ServiceProcessInstaller spInstaller;        private System.ServiceProcess.ServiceInstaller sInstaller;        ///          /// 清理所有正在使用的资源。        ///         /// 如果应释放托管资源,为 true;否则为 false。        protected override void Dispose(bool disposing)        {            if (disposing && (components != null))            {                components.Dispose();            }            base.Dispose(disposing);        }        #region 组件设计器生成的代码        ///         /// 设计器支持所需的方法 - 不要        /// 使用代码编辑器修改此方法的内容。        ///         private void InitializeComponent()        {            components = new System.ComponentModel.Container();            // 创建ServiceProcessInstaller对象和ServiceInstaller对象            this.spInstaller =new System.ServiceProcess.ServiceProcessInstaller();            this.sInstaller = new System.ServiceProcess.ServiceInstaller();            // 设定ServiceProcessInstaller对象的帐号、用户名和密码等信息            this.spInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;            this.spInstaller.Password = null;            this.spInstaller.Username = null;            // 设定服务的名称            this.sInstaller.ServiceName = "WindowsService1";            //设定服务启动的方式            this.sInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;            this.Installers.AddRange(new System.Configuration.Install.Installer[] {            this.spInstaller,this.sInstaller});        }        #endregion    }}5.生成工程,在bin目录下会生成exe文件。如果直接运行exe文件的话,是不能执行的,需要使用安装Windows服务用到一个名为InstallUtil.exe的命令行工具,打开命令行工具,转到InstallUtil.exe的目录下,我安装的是VS 2010,对应的目录为:C:WindowsMicrosoft.NETFrameworkv4.0.30319InstallUtil.exe,然后执行InstallUtil.exe 待执行的exe文件的目录,如:InstallUtil.exe F:MyProjectWindowsService1WindowsService1inDebugWindowsService1.exe。执行成功后,会在Windows的服务中,出现了刚刚添加的服务的名称。 6.启动该服务,这时打开binDebug文件夹,发现已经生成了一个test.txt的文件,里面记录了时间。这说明服务已经正式开始执行。7.停止服务的操作也和简单,打开命令行工具,转到C:WindowsMicrosoft.NETFrameworkv4.0.30319目录,然后执行InstallUtil.exe - u F:MyProjectWindowsService1WindowsService1inDebugWindowsService1.exe命令就可以了。

下载说明:请别用迅雷下载,失败请重下,重下不扣分!

发表评论

0 个回复

  • c++ 图像处理(旋转、水平镜像等)
    c++ 图像处理(旋转、水平镜像等)
    2019-12-12下载
    积分:1
  • STM32F103系列IO操作-LED流水灯
    STM32F103系列IO操作-LED流水灯
    2020-02-18下载
    积分:1
  • GC 垃圾回收算法
      =1,2垃圾回收算法:    托管堆:CLR要求的资源从托管堆分配,任何对象只要没有应用程序的根引用它,都会在某个时刻被垃圾回收器回收 基于代的机制,提高回收的性能,在程序的生命期中,新建的对象是新一代,而创建的比较早的对象是老一代,第0代是最近分配的对象,CLR 使用了0,1,2三代进行管理 =3 GC和调试GcAndDebug.cs =4本地资源终结(finalization)是CLR提供的一种机制,允许对象在垃圾回收器回收其内存之前执行一些得体的清理工作,任何包装了本地资源的类型都必须支持终结操作(实现一个命名为Finalize的方法)。GC判断一个对象是垃圾时,会调用对象的Finalize(实现的情况下),C#中使用~ClassName表示FinalizeIn most cases, you do not need to write classes that derive from the CriticalFinalizerObject class. The .NET Framework class library provides two classes, SafeHandle and CriticalHandle, that provide critical finalization functionality for handle resources. Furthermore, the .NET Framework provides a set of prewritten classes derived from the SafeHandle class, and this set is located in the Microsoft.Win32.SafeHandles namespace. These classes are designed to provide common functionality for supporting file and operating system handles. System.Runtime.ConstrainedExecution 下的 CriticalFinalizerObject 抽象对象,CLR特殊对待 System.Runtime.InteropServices下的派生抽象类 SafeHandle CriticalHandle 和SafeHandle只是引用计数的区别Microsoft.Win32.SafeHandles  SafeFileHandle,SafeRegistryHandle ,SafeWaitHandle... SafeProcessHandle SafeLibraryHandle SafeLocalMemHandle SafeThreadHandle等MSDN没有编写,但同样是处理这个问题的 类似的实现的不同类代表不同的资源类型,这么多的类似,主要是为了类型安全SafeHandle的作用:1、以前的IntPtr形式不够健壮,如在IntPtr赋值前可能抛出ThreadAbortException异常,这样托管代码将造成本地资源的泄露2、防止利用潜在的安全漏洞,一个线程访问一个本地资源,另一个线程释放该资源,通过引用计数实现 对托管资源只有在极少数情况下才使用终结器 终结器被调用的时刻:1、第0代满:2、显式调用GC.Collect3、Windows报告内存不足4、CLR卸载AppDomain5、CLR关闭 内部实现使用终结列表保留对象,通过这个GC进行处理, freachable队列 =Dispose终结器的调用时间由GC确定,调用者无法显式调用它Dispose提供了显式清理资源的能力 GCHandle WeakReference System.Runtime.CompilerServices.ConditionalWeakTable 加入时的是对象的WeakReference,因此可能会回收,但可以确保只要key存在,value也是存在的 任何大于85000字节的对象被自动认为是大对象,大对象在大对象堆中分配 ==大量资源GCpublic static void AddMemoryPressure(Int64 bytesAllocated);   public static void RemoveMemoryPressure(Int64 bytesAllocated);提示GC实际需要消耗的内存,然后GC监视内存压力,压力变大时,强制回收 System.Runtime.InteropServices  HandleCollector 数量有限的本地资源 MemoryFailPoint在需要大量内存前可以先检查是否可以使用这么大的内存 GC的监视代码形式: GC.GetTotalMemory GC.CollectionCountPerMon.exe系统性能监视工具CLR Profiler工具FUSLOGVW.execlrver.exeSvcTraceViewer.exe
    2012-12-16下载
    积分:1
  • c# 三层架构示例源码下载
    c# 三层架构示例源码下载
    2014-03-31下载
    积分:1
  • 串口调试助手代码(XCOM V2.0)
    【实例简介】 很优秀的串口调试助手代码
    2021-08-05 00:31:11下载
    积分:1
  • C++读取Windows系统硬盘信息
    C++语言读取Windows系统硬盘信息
    2019-12-05下载
    积分:1
  • winform listview美化 示例代码
    winform listview美化 示例代码
    2015-05-14下载
    积分:1
  • 嗨翻C.pdf
    程亦超老师翻译的一本关于C语言学习的书籍。 是我们学习C语言居家必备的书籍。本书向读者提供了C语言的完整学习体验。全书分为三个部分:第1章到第4章是基础知识,包括基本语法、指 针、字符串、小工具与源文件;第5章到第8章为进阶内容,有结构、联合、数据结构、堆、函数指针、动/静态链 接;最后四章是高级主题,内容涵盖了系统调用、进程间通信、网络编程和多线程。每部分结束后还有一个有趣 的实验,可以提高读者的实际操作能力。此外,书中还包含大量的图片、示例和代码,有助于读者对于知识的理 解和把握。 本书适用于C开发人员以及对C语言感兴趣的初学者。
    2021-05-06下载
    积分:1
  • 提取exe dll文件中的 资源图片文件例子源码下载
    提取exe dll文件中的 资源图片文件例子源码下载
    2015-04-29下载
    积分:1
  • C#全局钩子函数 例子源码下载
    C#全局钩子函数 例子源码下载
    2015-04-25下载
    积分:1
  • 696516资源总数
  • 106914会员总数
  • 0今日下载