简体   繁体   English

Windows服务和常规应用程序有什么区别?

[英]What is the difference between a windows service and a regular application?

I have only created regular windows applications (C# mostly). 我只创建了常规的Windows应用程序(主要是C#)。 What differentiates a windows service from a regular windows application? Windows服务与常规Windows应用程序的区别是什么? What makes them different? 是什么让他们不同? What can a service do that an application can't? 应用程序无法提供哪些服务? What are the differences seen from a developers point of view? 从开发人员的角度看有哪些不同之处? How do you create one? 你是如何创造一个的? Is it just to create a regular application (Console application maybe, since there are no gui?) and run or install it in a special way, or is it more that has to be done? 它只是创建一个常规应用程序(可能是控制台应用程序,因为没有gui?)并以特殊方式运行或安装它,还是需要做更多的事情?

There are a couple of things that jump out to me immediately. 有一些事情立即向我跳出来。

  • They run in an entirely different console starting with Vista 从Vista开始,它们在完全不同的控制台中运行
  • As a result of running in a different console, services cannot interact with the desktop. 由于在不同的控制台中运行,服务无法与桌面交互。 So essentially there is no direct UI support. 所以基本上没有直接的UI支持。 You typically have to code a sibling UI application that does run as a normal program and uses some mechanism (named pipes for example) to communicate with the service. 您通常必须编写一个兄弟UI应用程序,该应用程序作为普通程序运行,并使用某种机制(例如命名管道)与服务进行通信。
  • Typically only one instance of your service can be running at any given time. 通常,在任何给定时间只能运行一个服务实例。
  • Processes are per user, services are per work station and hence often provide services for multiple users. 进程是每个用户,服务是每个工作站,因此通常为多个用户提供服务。

This MSDN page leads to more documentation on creating them than you could shake a stick at. 这个MSDN页面提供了更多关于创建它们的文档,而不是你可以动摇一下。 This page is perhaps a better introduction to them in general. 这个页面可能是对它们的更好的介绍。

The key difference between a process running as an app versus as a service is that the service can operate entirely outside the normal association with a user and session. 作为应用程序运行的进程与作为服务运行的进程之间的主要区别在于,服务可以完全在与用户和会话的正常关联之外运行。 Thus services can run such that they start before any user logs in and can continue running after users log off. 因此,服务可以运行,以便它们在任何用户登录之前启动,并且可以在用户注销后继续运行。 Services are thus used to implement a great deal of the actual functionality of the operating system. 因此,服务用于实现操作系统的大量实际功能。

Services are also not tied to running as a 1:1 mapping with a process. 服务也不依赖于与进程的1:1映射运行。 Many services can exist within one process, normally through the use of svchost (take a look at these with process explorer for an indication of how this often works). 许多服务可以存在于一个进程中,通常通过使用svchost(使用进程资源管理器查看这些服务,以指示这通常如何工作)。 This reduces the effort at startup as multiple processes are not required for relatively lightweight services. 这减少了启动时的工作量,因为相对轻量级的服务不需要多个进程。

Implementing a service in c# is pretty simple, this page indicates how in very easy to follow terms. 在c#中实现服务非常简单,这个页面表明了如何非常容易地遵循术语。

Note that in reality a service in windows is little more that the scaffolding in the registry under HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Services which defines those 'image paths' (in most cases simply executables and the parameters to use) which are considered services along with which user then run as, which other services they depend on and whether they start at start up/post start up or as required. 请注意,实际上,Windows中的服务仅仅是HKEY_LOCAL_MACHINE \\ System \\ CurrentControlSet \\ Services下的注册表中的脚手架,它定义了那些被视为服务的“图像路径”(在大多数情况下只是可执行文件和要使用的参数)。然后,哪个用户运行,他们依赖哪些其他服务,以及他们是在启动/启动后启动还是根据需要启动。

If you're familiar with Unix, a Windows service is like a Unix daemon. 如果你熟悉Unix,那么Windows服务就像一个Unix守护进程。 It isn't associated with any particular user, and is always running in the background. 它与任何特定用户无关,并且始终在后台运行。

The main difference is that a windows service is something you want to run as a background service and doesn't require a UI. 主要区别在于,您希望将Windows服务作为后台服务运行,而不需要UI。 An example is a service that indexes files on your drive for searching. 一个示例是一个索引驱动器上的文件以进行搜索的服务。

Another benefit is you can have services automatically start when the user logs in. 另一个好处是您可以在用户登录时自动启动服务。

There are also methods you can override that are called when the service is started/stopped (ie. from Control Panel | Administrative Tools | Services). 还有一些方法可以覆盖在服务启动/停止时调用的方法(即从“控制面板”|“管理工具”|“服务”)。

Within Visual Studio there is a special project type you can use to create it. 在Visual Studio中,有一个特殊的项目类型可用于创建它。 See the site below for an example: http://www.dotheweb.net/articles/dotnet/services.aspx 请参阅以下网站以获取示例: http//www.dotheweb.net/articles/dotnet/services.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “windows应用程序”和“Windows窗体应用程序”有什么区别 - What is the difference between “windows application” and “windows forms application” 启动时控制台应用程序和.NET中的Windows应用程序有什么区别 - What is the difference between Console Application and Windows Application in .NET at startup _Application和Application之间有什么区别 - What is the difference between _Application and Application “服务”和“组件”有什么区别? - What is the difference between "service" and "component"? 常规字符串和逐字字符串有什么区别? - What is the difference between a regular string and a verbatim string? DDD服务和WCF服务之间有什么区别? - What is the difference between a DDD service and a WCF service? Windows 服务的 ResumeAutomatic 和 ResumeSuspend 模式之间的区别 - Difference between ResumeAutomatic & ResumeSuspend modes of Windows Service WCF:IIS和WIndows服务托管之间的区别 - WCF: Difference between hosting in IIS and WIndows service Windows服务和Windows窗体应用程序之间的通信 - Communication between Windows service and Windows form application Windows应用程序中平台与目标平台的区别 - Difference between Platform and Target Platform in windows Application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM