如何用C#创建Windows Service

LVGL实战 透视卡盘 八张卡立在一个圆柱面上,摄像机在柱心前方。每帧对屏幕上的每个像素发一条射线,与卡片所在平面求交,得到三维交点,再投影回卡片自己的二维坐标系取样。透视、倾斜、近大远小、相互遮挡 —— 全部是这一步的副产物。 阅读详情

在.Net中用C#创建Windows Service,其实很简单,按照以下的步骤就可以做出一个简单的Windows Service。

1.首先在创建工程的时候选择Windows Service,这样.Net会自动生成Windows Service的框架;

2.完成Windows Service的相应事件,主要是OnStart和OnStop这两个事件,完成后大致代码如下:

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Diagnostics;

using System.ServiceProcess;

using System.IO;

using System.Threading;

namespace WinSDemo

{

    public class WinSDemo : System.ServiceProcess.ServiceBase

    {

        /// <summary>

        /// Required designer variable.

        /// </summary>

        private System.ComponentModel.Container components = null;

        private bool blnStopThread;

        private Thread thdMain;

 

        public WinSDemo()

        {

            // This call is required by the Windows.Forms Component Designer.

            InitializeComponent();

 

            // TODO: Add any initialization after the InitComponent call

        }

 

        // The main entry point for the process

        static void Main()

        {

            System.ServiceProcess.ServiceBase[] ServicesToRun;

   

            // More than one user Service may run within the same process. To add

            // another service to this process, change the following line to

            // create a second service object. For example,

            //

            ServicesToRun = new System.ServiceProcess.ServiceBase[] { new WinSDemo() };

 

            System.ServiceProcess.ServiceBase.Run(ServicesToRun);

        }

 

        /// <summary>

        /// Required method for Designer support - do not modify

        /// the contents of this method with the code editor.

        /// </summary>

        private void InitializeComponent()

        {

            //

            // WinSDemo

            //

            this.CanPauseAndContinue = true;

            this.ServiceName = "MyTest";

 

        }

 

        /// <summary>

        /// Clean up any resources being used.

        /// </summary>

        protected override void Dispose( bool disposing )

        {

            if( disposing )

            {

                if (components != null)

                {

                    components.Dispose();

                }

            }

            base.Dispose( disposing );

        }

 

        /// <summary>

        /// Set things in motion so your service can do its work.

        /// </summary>

        protected override void OnStart(string[] args)

        {

            // TODO: Add code here to start your service.

 

            thdMain=new Thread(new ThreadStart(WriteLog));

            thdMain.Start();

        }

 

        /// <summary>

        /// Stop this service.

        /// </summary>

        protected override void OnStop()

        {

            // TODO: Add code here to perform any tear-down necessary to stop your service.

            blnStopThread=true;

            thdMain.Join();

        }

 

        protected override void OnPause()

        {

            thdMain.Suspend();

        }

 

        protected override void OnContinue()

        {

            thdMain.Resume();

        }

 

        protected void WriteLog()

        {

            StreamWriter myWriter=null;        

            do

            {

                Process.Start("net","send localhost yourValue");

                try

                {

                    myWriter=new StreamWriter("c://MyLog.txt",true);

                    myWriter.WriteLine(DateTime.Now.ToString());

                    myWriter.Close();

                }

                catch{};

 

                Thread.Sleep(5000);

            }while(blnStopThread==false);

           

        }

    }

}

       注:为了使自己能更好的识别自己写的Windows Service,建议在InitializeComponent修改Service的名称。

 

3.为了使自己写的Service能加载到系统中去,光靠以上步骤是不够;接下来,向当前的工程添加Service Installer,在其中设置Service安装后的起始状态,代码如下:

using System;

using System.Collections;

using System.ComponentModel;

using System.Configuration.Install;

using System.ServiceProcess;

 

 

namespace WinSDemo

{

    /// <summary>

    /// Summary description for WinSDemoIns.

    /// </summary>

    [RunInstaller(true)]

    public class WinSDemoIns : System.Configuration.Install.Installer

    {

        /// <summary>

        /// Required designer variable.

        /// </summary>

        private System.ComponentModel.Container components = null;

        private ServiceInstaller serviceInstaller;

        private ServiceProcessInstaller processInstaller;

 

        public WinSDemoIns()

        {

            // This call is required by the Designer.

            InitializeComponent();

 

            // TODO: Add any initialization after the InitComponent call

            processInstaller = new ServiceProcessInstaller();

            serviceInstaller = new ServiceInstaller();

 

            // Service will run under system account

            processInstaller.Account = ServiceAccount.LocalSystem;

 

            // Service will have Start Type of Manual

            serviceInstaller.StartType = ServiceStartMode.Automatic;

 

            serviceInstaller.ServiceName = "MyTest";

 

            Installers.Add(serviceInstaller);

            Installers.Add(processInstaller);

        }

 

        #region Component Designer generated code

        /// <summary>

        /// Required method for Designer support - do not modify

        /// the contents of this method with the code editor.

        /// </summary>

        private void InitializeComponent()

        {

            components = new System.ComponentModel.Container();

        }

        #endregion

    }

}

 

4.完成以上的步骤,代码的部分就完成了,编译成可执行文件,再用.Net的Service安装工具就行了,即在Dos窗口中,键入“installutil yourService.exe”,这样执行就可以了,相反,如果想卸载Service的话,加一个参数就可以了,即“installutil /u yourService.exe”。注意有可能.Net的路径在环境变量中不存在,可能直接执行是不能成功的,希望先找到“installutil.exe”存在的目录,大致在“/WINDOWS/Microsoft.NET/Framework/v1.1.4322”目录下。

 

至于以后Service的部署,由于.Net写的程序,运行环境必须要安装.Net Framework,所以在其他机器安装自己写的Service时候,一定要先安装.Net运行环境。

 

华为od 面试八股文_测试/测试开发_02_含答案 阅读详情

相关推荐

[CTFSHOW]JWT

文章目录前言JWT简介CTFSHOW Web345CTFSHOW Web346前置知识开始做题CTFSHOW Web347CTFSHOW Web348CTFSHOW Web349CTFSHOW Web350前置知识利用WP如何防御 前言 有些师傅向我咨询WP,索性就发了吧,其实早就写好了,只是懒,在github仓库里囤着 JWT简介 可以看看我的Github总结给自己看的,Y4tacker CTFSHOW Web345 首先打开题目,查看源码,接下来访问/admin被跳转回主页啥也没有发生 where is

Y4tacker的博客 6044

C# windows服务创建、安装、启动和卸载

一、windows服务的创建 在vs创建项目中直接创建一个windows服务项目   在Service1.cs中右键查看代码,就可以看到OnStart、OnStop   两个方法,OnStart 是服务启动时执行的代码;OnStop 是服务停止时执行的代码,如下图:   在OnStart 中写入以下代码:   using System; using System.Coll...

霏段的博客 1549

从原理到实战:ARS548 4D毫米波雷达数据处理与多模态融合全解析

本文深入解析了4D毫米波雷达ARS548的工作原理与数据处理全流程。从FMCW测距测速、MIMO虚拟天线阵列到点云生成的三步FFT处理,提供了详尽的Python代码实现。文章进一步探讨了与激光雷达、相机的多模态时空对齐与融合策略,并指导如何构建用于自动驾驶感知训练的多模态标注数据集,为相关算法工程师提供了从理论到实践的完整指南。

weixin_29161719的博客 366

service启动方式

两种启动方式: 1. start方式 onCreate()--&gt;onStartCommand(Intent intent, int flags, int startId)--&gt;onDestroy() a.某个servie 在整个应用中只有一个 可以此activity启动,另一个stop b.只在第一次启动时调用onCreate,其他时候调用(一个或多个activi...

dawan19909的博客 304

“基于车载车机 ADB 的安卓自动化测试”以及“类似智谱 AutoGLM 的 AI 安卓自动化项目”的整理和选型建议。

本文系统梳理了安卓自动化测试两大路线。传统ADB框架中,**Appium**适用于企业级测试,**AndroidViewClient**和**Airtest**适合脚本化与图像识别场景。AI驱动的方案里,**智谱Open-AutoGLM**擅长中文场景的视觉理解与操作,**DroidRun**支持多模型与跨平台,而**Android-MCP**则适合将安卓能力作为工具集成到现有AI平台。选型需根据稳定性、技术栈与AI集成需求决定。

weixin_39072720的博客 899

免Root抓包神器Packet Capture:安卓App数据捕获全攻略

本文详细介绍了免Root安卓抓包神器Packet Capture的安装与使用全攻略。通过利用安卓系统的VPN Service API,该工具无需Root权限即可捕获并解密App的HTTP/HTTPS网络流量。文章从核心原理、证书安装、实战抓包步骤到结果分析与常见问题排查,提供了完整指南,是移动端开发、安全测试和数据分析的实用利器。

weixin_29232507的博客 1073

调试windows service的OnStart事件及调试service的一些方法汇总

关于调试windows service, 其实这是一个老生常谈的问题了.    通常的处理办法是, 在service运行后, 在调试器中选择attach to process.    然而这种做法也有一定的局限性, 例如在service启动时的OnStart事件中的代码, 基本上很难调试. 往往当attach到我们的service的时候, 这部分代码已经执行过了. 于是, 有人提出, 可以

jiangxinyu的专栏 1万+

ESP32系列之LVGL(四):输入设备(外部按键)对接

本文详细介绍了如何在ESP32上使用LVGL库对接外部按键作为输入设备,包括硬件连接、驱动层实现和LVGL输入设备移植。通过具体代码示例和实战案例,帮助开发者快速实现按键控制UI界面,解决常见问题如按键无反应和控件跳转不正常。

weixin_30907935的博客 241

当Windows操作系统关机时,不会执行Windows Service的OnStop方法(转载)

Windows Service OnStop when computer shutdown 问: I'm writing a Windows Service in C#. I want to take the same action for when the service is stopped by the Service control panel as w...

899

Python爬虫实战——如何自动爬取百度搜索结果页面

本文介绍了如何使用Python爬虫抓取百度搜索结果页面的内容,包括如何构建搜索URL、获取页面HTML、解析内容、处理翻页以及存储数据等。通过该教程,您可以轻松实现批量抓取百度搜索结果,并为后续的数据分析和处理打下基础。希望本文能为您在爬虫开发、数据分析等领域提供帮助。如果您在实际应用过程中遇到问题,可以参考反爬虫应对策略,并结合实际需求进行调整。

2201_76125261的博客 1711

全网首发--Unet3+改进16:在不同位置添加RepLKBlock超大卷积核

本文提出了一种基于大核卷积的神经网络架构RepLKNet,旨在缩小CNN与ViTs之间的性能差距。通过重新审视CNN中的大核设计,研究证明使用少量大卷积核替代多个小核能获得更强大的性能。提出的RepLKNet采用31×31大卷积核,在ImageNet和下游任务上取得了与SwinTransformer相当或更好的结果,同时具有更低延迟。文章详细介绍了实现步骤:(1)创建RepLKBlock模块文件;(2)在模型层中添加注意力机制;(3)在UNet模型中集成RepLKBlock;(4)完成训练流程。该方法展现了

AIcurator的博客 616

C# 创建Windows Service服务 Windows服务

Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的。所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Windows Service写很深入。 本文介绍了如何用C#创建、安装、启动、监控、卸载简单的Windows Service 的内容步骤和注意事项。 一、创建一个Windows Service 1)创建W

邓杨喜 的专栏 909

CAPL如何设置或修改CANoe TCP/IP协议栈的底层配置

MSS = MTU - tcpHeader - ipHeader,也就是MTU = MSS(1360) + tcpHeader(20) + ipHeader(20) = 1400,说明CAPL是可以动态设置网络底层配置参数的!然后在CAPL中设置此网卡MTU值为1400,然后创建UDP套接字,一次性发送2000个字节,在Trace窗口看看首帧报文二层Data的长度。所以我们还不能用UDP协议来验,应该用TCP协议来验,也就是在TCP三次握手时通过Tester的MSS值推导出自己的MTU值。

jasonj333 992

用C#创建Windows Service

用C#创建Windows Service 现在的.NET框架已经为Windows service的开发提供足够强大的支持,你只需要关注service所要实现的逻辑,而完全不用关心service底层是如何实现的,相比以前用MFC来说,真是质的飞跃啊。闲话少谈,看看如何实现一个最基本的windows service,以及如何来调试windows service。

mmpire之blog 1000

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

C#创建Windows Service

转自 http://www.cnblogs.com/sorex/archive/2012/05/16/2502001.html Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的。所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Windows Service写很深入。 本文介绍了如何用

liushaoming 383

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

C# Windows Service服务的创建和调试

C# Windows Service服务的创建和调试

青阳十五的专栏 427

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

C#创建Windows Service(Windows 服务)基础教程

Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的。所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Windows Service写很深入。 本文介绍了如何用C#创建、安装、启动、监控、卸载简单的Windows Service 的内容步骤和注意事项。 一、创建一个Windows Service ...

weixin_30287169的博客 81

C# windows service 创建 、调试、部署

查看windows service WIN+R 输入 services.msc 也可 右击我的电脑 》属性 》控制面板 》 找到管理工具 》双击 》 找到服务 ,事件查看器可以查看 service 运行 的事件 日志 方框对应下文中serviceIntaller.cs 中的 Description(系统服务的描述) DisplayName (系统服务...

新新的博客 9404

c#创建windows service示例

有关windows service的详细介绍,不在本文叙述。如果想了解,自己到网上去搜一搜,关键词就是“windows service”,搜索结果保证不会让你失望,呵呵!本文的预期读者即为对windows service有一定了解,但又没有编写过windows service程序的人。首先咱不急于介绍如何如何编码,第一步干吗第二步干吗。在你需要动手之前,你要想清楚,我为什么要这么做,有没有其它更方

1789

【windows service】C# 创建Windows Service(Windows服务)程序

很多年前大概08年做了一个windows桌面程序推送邮件给用户的程序,然后通过windows 计划任务每隔N分钟重复执行。 今天闲暇无聊,想下能否把桌面程序封装成windows service程序呢?那就动手试试吧。双击service1.cs 在显示的界面鼠标右键选择添加安装程序 分别配置serviceProcessInstaller和serviceInstaller的属性 OnStart :服务启动的时候执行,OnStop:服务停止的时候执行,我们在启动和停止执行时候写入日志 生成项目解决方案,直接打开

德仔 5476
上一篇: 如何使用Base64进行加密和解密
下一篇: 如何在DataGrid中进行添加、删除和修改操作
Knight94
博客等级 码龄25年 1013粉丝 87原创
评论 24
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值