Simple Windows Service Sample

RustFS Windows部署指南:分布式文件系统抽象层实战 文件系统抽象层(FAL)是现代存储中间件实现跨平台、高并发与强一致性的关键技术基础。其核心原理在于将底层IO操作与业务逻辑解耦,通过内存安全的接口调度磁盘、网络或内存设备。Rust语言凭借零成本抽象、无GC停顿和细粒度控制能力,成为构建高性能FAL的理想选择。在Windows环境下,RustFS作为典型Rust实现,不仅支持IOCP与内存映射等原生机制,更提供BLAKE3校验、WAL日志和原子写入等企业级数据可靠性保障,广泛应用于边缘缓存、工业数据采集与金融日志归档等对崩溃一致性要求严苛的场景。 阅读详情

Introduction

As a matter of fact Microsoft Windows services, formerly known as NT services enable you to create long-running executable applications that run in its own Windows session, which then has the ability to start automatically when the computer boots and also can be manually paused, stopped or even restarted.

This makes services ideal for use on a server or whenever you need long-running functionality that does not interfere with other users who are working on the same computer. You can also run services in the security context of a specific user account that is different from the logged-on user or the default computer account.

Windows services don’t have any interface to the user, so it can not be debugged like any regular application, but it’s debugged as a process. .NET has a very nice tool that enables processes debugging while it’s in the run status, by easily pressing Ctrl + Alt + P shortcut.

Background

I’ve searched well so many sites about a code that I can with the help of it, build a simple Windows service, but I found a lot of code on how to manage the current Windows services of the system and that’s through the ServiceController class.

After searching the MSDN, I’ve found some nice code that helped me to create this simple Windows service. Hope it can help as a basic architecture for and usage of such a Windows service.

Using the code

At first you should simply open VS.NET and then at the File menu click on New, Project. From the New Project Dialog Box, choose the Windows service template project and name it MyNewService like shown below:

Winows Service New Project

The project template automatically adds a component class that is called Service1 by default and inherits from System.ServiceProcess.ServiceBase.

Click the designer. Then, in the Properties window, set the ServiceName property for Service1 to MyNewService.

Set the Name property to MyNewService. Set the AutoLog property to true.

In the code editor, edit the Main method to create an instance of MyNewService. When you renamed the service in step 3, the class name was not modified in the Main method. To access the Main method in VC#, expand the Component Designer generated code region.

static void Main()
{ 
    System.ServiceProcess.ServiceBase[] ServicesToRun; 
    //Change the following line to match. 
    ServicesToRun = new 
        System.ServiceProcess.ServiceBase[] { new MyNewService() }; 
    System.ServiceProcess.ServiceBase.Run(ServicesToRun);  
}

In the next section, you will add a custom event log to your Windows service. Event logs are not associated in any way with Windows services. Here the EventLog component is used as an example of the type of components you could add to a Windows service.

To add custom event log functionality to your service:

  1. In the Solution Explorer, right-click Service1.vb or Service1.cs and select View Designer.
  2. From the Components tab of the Toolbox, drag an EventLog component to the designer.
  3. In the Solution Explorer, right-click Service1.vb or Service1.cs and select View Code.
  4. Edit the constructor to define a custom event log.

To access the constructor in Visual C#, expand the Component Designer generated code region.

public MyNewService()
{
 
    InitializeComponent()
    if(!System.Diagnostics.EventLog.SourceExists("DoDyLogSourse"))
    System.Diagnostics.EventLog.CreateEventSource("DoDyLogSourse",
                                                          "DoDyLog");

    eventLog1.Source = "DoDyLogSourse";
    // the event log source by which 

    //the application is registered on the computer

    eventLog1.Log = "DoDyLog";
}

To define what happens when the service starts, in the code editor, locate the OnStart method that was automatically overridden when you created the project, and write code to determine what occurs when the service begins running:

protected override void OnStart(string[] args)
{ 
    eventLog1.WriteEntry("my service started"); 
}

The OnStart method must return to the operating system once the service's operation has begun. It must not loop forever or block. To set up a simple polling mechanism, you can use the System.Timers.Timer component. In the OnStart method, you would set parameters on the component, and then you would set the Timer.Enabled property to true. The timer would then raise events in your code periodically, at which time your service could do its monitoring.

To define what happens when the service is stopped, in the code editor, locate the OnStop procedure that was automatically overridden when you created the project, and write code to determine what occurs when the service is stopped:

protected override void OnStop()
{ 
    eventLog1.WriteEntry("my service stoped");
}

You can also override the OnPause, OnContinue, and OnShutdown methods to define further processing for your component. For the method you want to handle, override the appropriate method and define what you want to occur. The following code shows what it looks like if you override the OnContinue method:

protected override void OnContinue()
{
    eventLog1.WriteEntry("my service is continuing in working");
}

Some custom actions need to occur when installing a Windows service, which can be done by the Installer class. Visual Studio can create these installers specifically for a Windows service and add them to your project. To create the installers for your service.

  1. Return to design view for Service1.
  2. Click the background of the designer to select the service itself, rather than any of its contents.
  3. In the Properties window, click the Add Installer link in the gray area beneath the list of properties. By default, a component class containing two installers is added to your project. The component is named ProjectInstaller, and the installers it contains are the installer for your service and the installer for the service's associated process.
  4. Access design view for ProjectInstaller, and click ServiceInstaller1.
  5. In the Properties window, set the ServiceName property to MyNewService.
  6. Set the StartType property to Automatic.

Tip

To avoid being asked about the system username and password you must change the Account for the serviceProcessInstaller to LocalSystem. This is done by opening the ProjectInstaller design and then selecting the serviceProcessInstaller, press F4 and then change the Account property to LocalSystem. Or you can manually do that by creating a class that inherits from System.Configuration.Install.Installer like this:

Collapse
[RunInstaller(true)]

public class ProjectInstaller : System.Configuration.Install.Installer 
private System.ServiceProcess.ServiceProcessInstaller 
                                  serviceProcessInstaller1;
private System.ServiceProcess.ServiceInstaller serviceInstaller1; 
/// <summary> 
/// Required designer variable. 
/// </summary> private System.ComponentModel.Container components = null;

public ProjectInstaller()
   // This call is required by the Designer.
   InitializeComponent();

   // TODO: Add any initialization after the InitComponent call 
} 
private void InitializeComponent() 
{ 
   this.serviceProcessInstaller1 = 
     new System.ServiceProcess.ServiceProcessInstaller(); 
   this.serviceInstaller1 = 
     new System.ServiceProcess.ServiceInstaller(); 
   // serviceProcessInstaller1 
   // 
   this.serviceProcessInstaller1.Account = 
     System.ServiceProcess.ServiceAccount.LocalSystem; 
   this.serviceProcessInstaller1.Password = null;
   this.serviceProcessInstaller1.Username = null; 
   // 
   // serviceInstaller1 
   // 
   this.serviceInstaller1.ServiceName = "MyNewService"; 
   this.serviceInstaller1.StartType = 
     System.ServiceProcess.ServiceStartMode.Automatic;

   // 
   // ProjectInstaller 
   // 
   this.Installers.AddRange
     (new System.Configuration.Install.Installer[] 
   { 
       this.serviceInstaller1, 
       this.serviceInstaller1});
   }
}

To build your service project

  1. In Solution Explorer, right-click your project and select Properties from the shortcut menu. The project's Property Pages dialog box appears.
  2. In the left pane, select the General tab in the Common Properties folder.
  3. From the Startup object list, choose MyNewService. Click OK.
  4. Press Ctrl+Shift+B to build the project. 

Service Project Property Page

Now that the project is built, it can be deployed. A setup project will install the compiled project files and run the installers needed to run the Windows service. To create a complete setup project, you will need to add the project output, MyNewService.exe, to the setup project and then add a custom action to have MyNewService.exe installed.

To create a setup project for your service

  1. On the File menu, point to Add Project, and then choose New Project.
  2. In the Project Types pane, select the Setup and Deployment Projects folder.
  3. In the Templates pane, select Setup Project. Name the project MyServiceSetup.

A setup project is added to the solution. Next you will add the output from the Windows service project, MyNewService.exe, to the setup.

Service Setup Project

To add MyNewService.exe to the setup project

  1. In Solution Explorer, right-click MyServiceSetup, point to Add, then choose Project Output. The Add Project Output Group dialog box appears.
  2. MyNewService is selected in the Project box.
  3. From the list box, select Primary Output, and click OK.

    A project item for the primary output of MyNewService is added to the setup project. Now add a custom action to install the MyNewService.exe file.

To add a custom action to the setup project

  1. In Solution Explorer, right-click the setup project, point to View, then choose Custom Actions. The Custom Actions editor appears.
  2. In the Custom Actions editor, right-click the Custom Actions node and choose Add Custom Action. The Select Item in Project dialog box appears.
  3. Double-click the application folder in the list box to open it, select primary output from MyNewService (Active), and click OK. The primary output is added to all four nodes of the custom actions — Install, Commit, Rollback, and Uninstall.
  4. Build the setup project.

To install the Windows Service

Browse to the directory where the setup project was saved, and run the .msi file to install MyNewService.exe.

Service Setup

To start and stop your service

  1. Open the Services Control Manager by doing one of the following:
    • In Windows 2000 Professional, right-click My Computer on the desktop, then click Manage. In the Computer Management console, expand the Services and Applications node.

      - Or -

    • In Windows 2000 Server, click Start, point to Programs, click Administrative Tools, and then click Services.

      Note: In Windows NT version 4.0, you can open this dialog box from Control Panel.

  2. You should now see MyNewService listed in the Services section of the window.
  3. Select your service in the list, right-click it, and then click Start.

Right-click the service, and then click Stop.

Admin tools Services

To verify the event log output of your service

  1. Open Server Explorer and access the Event Logs node. For more information, see Working with Event Logs in Server Explorer.

    Note: The Servers node of Server Explorer is not available in the Standard Edition of Visual Basic and Visual C# .NET.

    Sample screenshot

To uninstall your service

  • On the Start menu, open Control Panel and click Add/Remove Programs, and then locate your service and click Uninstall.
  • You can also uninstall the program by right-clicking the program icon for the .msi file and selecting Uninstall.
 
AI三端逆向SKILL:Web/Android/Windows平台智能辅助逆向工程实践指南 逆向工程是安全研究中的核心技术,涉及对软件、协议或系统进行反向分析以理解其工作原理、发现潜在漏洞或进行安全评估。其核心原理是通过静态分析、动态调试等技术手段,还原程序的逻辑结构与算法实现。随着软件保护技术的复杂化,传统逆向工程面临效率瓶颈,而人工智能技术的引入为解决这一挑战提供了新思路。AI辅助逆向通过机器学习模型识别代码模式、推测加密算法、解释反汇编指令,能够显著提升分析效率并降低对经验的高度依赖,在代码审计、漏洞挖掘、恶意软件分析等场景中展现出重要技术价值。本文聚焦于一个覆盖Web前端混淆、Androi 阅读详情

相关推荐

离线部署小米TTS技能:本地化语音合成与智能家居集成实战

语音合成(TTS)技术是实现人机语音交互的核心,其原理是将文本信息通过声学模型和声码器转换为自然流畅的语音波形。这项技术的价值在于为智能设备赋予“说话”能力,广泛应用于智能助手、无障碍服务、车载系统和内容播报等场景。随着对数据隐私和网络稳定性的需求增长,本地化、离线可用的TTS方案成为技术热点。本文聚焦于如何利用开源VITS模型和PaddleSpeech等工具,在Windows等环境中构建一个高性能、可集成的本地TTS服务,并自然融入智能家居生态。通过实战演示,读者将了解如何解决模型部署、服务封装和性能优化

weixin_30911451的博客 315

msdn中windows service例子

http://code.msdn.microsoft.com/CppWindowsService-74bb412c   A basic Windows service in C++ (CppWindowsService) This code sample demonstrates creating a basic Windows Service application in VC++

孙建军的博客 3687

Grok智能体后台模式配置指南:实现24小时AI自动化任务部署

AI智能体作为自动化技术的核心组件,通过模拟人类操作实现业务流程自动化。其技术原理基于深度学习和计算机视觉,能够实时解析屏幕内容并生成相应的操作指令。在工程实践中,AI智能体的价值在于提升工作效率、减少人工干预,特别适用于数据整理、系统监控等重复性任务场景。Grok智能体采用系统2指挥系统1的架构设计,其中后台模式的支持让系统更适合企业级部署。通过守护进程和系统服务方式运行,Grok智能体能够实现24小时不间断工作,满足企业自动化和个人生产力助手等需求。本文重点介绍的后台模式配置,正是基于这一技术架构,为长

weixin_30682415的博客 320

sample.exe 病毒分析

0x0 病毒介绍 通过局域网传播的病毒,通过接受指令完成恶意行为,下载文件更新病毒。   0x1 概述 样本名:sample.exe MD5:f3b51ce3878eb7a6b2c5f5ff5de0c7b5 壳或者编译信息:PACKER:UPX 0.89.6 - 1.02 / 1.05 - 1.24 ->Markus & Laszlo 分析环境及工具:winXp sp3、IDA6.8

AppleDir的博客 2459

无需显卡,纯CPU内存本地部署大模型:技术原理与实战指南

模型量化是一种通过降低模型参数精度来压缩模型大小、减少计算和存储需求的关键技术。其核心原理是将高精度浮点数(如FP32)转换为低精度表示(如INT8、INT4),在保持模型能力基本不变的前提下,显著降低对内存带宽和并行计算单元的依赖。这项技术的价值在于大幅降低了AI模型部署的硬件门槛和成本,使得在资源受限的边缘设备或个人电脑上运行大模型成为可能。应用场景广泛,包括本地私有化AI助手、边缘计算设备集成、教育实验环境以及注重数据隐私的企业内部流程。通过结合GGUF格式和llama.cpp等高效推理框架,开发者可

weixin_34195364的博客 364

Claude与Codex语音交互技术对比:架构差异与选型指南

语音交互技术通过语音识别、自然语言处理和语音合成等核心组件,实现了人机自然对话的能力。其技术原理基于深度学习模型对音频信号进行分析和理解,将语音转换为文本并进行语义解析。在工程实践中,语音交互的价值体现在提升用户体验、提高操作效率和拓展应用场景等方面,广泛应用于智能客服、教育辅助、智能家居等领域。从技术架构角度,一体化方案如Claude提供了完整的端到端解决方案,简化了部署流程;而模块化设计如Codex则强调灵活性和扩展性,支持定制化开发。本文基于claude desktop安装和codex插件等热词,深入

weixin_30448603的博客 300

Service 使用

Service 基本用法 ,启动、关闭 Service

伯特立的专栏 2420

WebServiceSample

WSDL+RestFul

Android 学习笔记 Service (二) Remote Service Sample

1.一步步的建立Remote Service 0)创建一个helloworld项目。 1)新建文件 IRemoteService.aidl 为什么要建aidl文件,因为在这里我们使用的是Remote Service,从字面Remote就知道Service和Client是分开的(这里我们为了演示方便,把Service和Client的实现都放在一起,但是要时刻想到它们是不在一起的)。 定义Se

Dake 6034

一个很完整很强大的Windows服务程序框架

一个很完整很强大的Windows服务程序框架,用C++实现的,特别强大与方便。所有你想要的函数都有,想怎么填怎么填。命令行也很强大,足够你用了。

ServiceSample

ServiceSample

Indexing Service simple sample

Indexing Service 简单例子

Ivan Song的专栏 1406

OAuth WCF Service Sample

自定义OAuth服务端(WCF REST数据访问控制) 详见:http://blog.csdn.net/fangxinggood/article/details/6647672

SlimMessageBus是什么?全面解析这款支持Kafka、RabbitMQ等10+中间件的.NET轻量级消息总线

SlimMessageBus 是一款免费开源、面向 .NET 生态的轻量级消息总线(Message Bus)框架,原生支持 Pub/Sub(发布/订阅)与 Request-Response(请求/响应)两大异步通信模式。它可以通过传输插件接入 Apache Kafka、RabbitMQ、Azure Service Bus、Redis、MQTT、NATS 等 10 多种主流消息中间件,让业务代码只写

gitblog_01050的博客 672

sample_service

sample_service

PLSQL Developer新手必看:如何避免安装过程中的常见坑

本文为PL/SQL Developer新手提供了详尽的安装与配置避坑指南。从版本选择、注册码使用、汉化包安装到Oracle客户端连接配置,逐一解析常见问题并提供解决方案,帮助开发者快速搭建稳定的PL/SQL开发环境,顺利开启Oracle数据库编程工作。

g9h0i1的博客 650

DeepSeek-OCR 3分钟部署实战:避坑指南与生产落地

OCR(光学字符识别)是文档数字化的核心技术,其底层依赖深度学习框架(如PyTorch)、多模态模型(Layout Detection + Text Recognition)及CUDA加速能力。理解OCR系统部署的本质,不是简单执行pip install,而是构建Python解释器、CUDA工具链、模型权重与推理管道的严格兼容链路。本文聚焦DeepSeek-OCR这一轻量级开源OCR框架,围绕torch CUDA版本对齐、conda环境隔离、Hugging Face模型离线加载、GPU显存优化等高频痛点,提

weixin_30247307的博客 333

超级详细的H3C模拟器HCL的交换机Telnet服务配置过程(图文)

VC++2017 MFC关于一个电话机房配线架进线出线节点数据库管理的程序 机房数据库原来是一个EXCEL表,查询,输入数据不方便。现用access数据库ODBC访问。平常没编过程序,编个程序练练手。记录一下学习中的问题,和解决方法,供大家分享,讨论,以便共同进步。希望多提宝贵意见,携手提高代码水平。 程序写的很不规范,代码冗余,显得程序臃肿,代码重用性差。发表这篇博客的目的也是为了改进这个程序。CListCtrl 单元格可以编辑,有Combo Box下拉列表功能。使用PreTranslateMe

techcai的博客 1万+

Python开发环境管理革命:uv工具链构建现代化AI项目工作流

Python依赖管理与虚拟环境配置是每个开发者必须掌握的基础技能,它直接关系到项目的可复现性和开发效率。传统基于pip和venv的方案在面临多版本Python共存、依赖冲突和跨平台一致性等问题时显得力不从心。现代Python生态通过一体化工具链解决了这些痛点,其中uv作为由Rust编写的高性能包管理器,将Python版本管理、虚拟环境创建、依赖解析锁定等功能整合于一身,实现了数量级的性能提升。这种技术方案特别适用于AI和数据科学领域,能够快速搭建包含PyTorch、Transformers等大型框架的可靠环

weixin_30386713的博客 568

【AI深究】合成数据(Synthetic Data)深度解析:原理、算法与工程实践——全网最详细流程|核心原理、主流方法、数学表达、工程实践与细节、未来趋势|GAN、VAE、语言模型LLM合成如GPT

大家好,我是爱酱。本篇延续将会系统梳理合成数据(Synthetic Data)的核心原理、主流方法、数学表达、工程实践与未来趋势,结合数学公式,帮助你全面理解这一AI建模的关键技术。 注:本文章含大量数学算式、详细例子说明及大量代码演示,大量干货,建议先收藏再慢慢观看理解。新频道发展不易,你们的每个赞、收藏跟转发都是我继续分享的动力!

AI人工智能爱酱~你的AI学习好帮手~ 4921

【愚公系列】《AIGC辅助软件开发》041-如何使用 AI技术提升工作效率:AI辅助软件架构师实践

在数字化转型的浪潮中,软件架构师作为技术团队的核心,承载着系统设计与架构优化的重要责任。然而,随着项目复杂度的增加和技术环境的变化,架构师面临着前所未有的挑战。幸运的是,人工智能(AI)技术的迅猛发展为这一行业带来了新的机遇,帮助架构师提升工作效率、优化决策过程,并改善团队协作。本文将深入探讨AI如何辅助软件架构师的实际工作,具体分析AI在需求分析、架构设计、代码生成、测试和优化等环节的应用。

愚公智库 15万+
上一篇: 在vs6.0和vs2005中安装STLPort
下一篇: 系统架构师是怎样炼成的?
suacker
博客等级 码龄24年 46粉丝 19原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值