Windows 服务和常规应用程序有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1217655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between a windows service and a regular application?
提问by Svish
I have only created regular windows applications (C# mostly). What differentiates a windows service from a regular windows application? 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?
我只创建了常规的 Windows 应用程序(主要是 C#)。Windows 服务与常规 Windows 应用程序的区别是什么?是什么让他们与众不同?服务可以做什么而应用程序不能做什么?从开发人员的角度来看,有哪些不同之处?你如何创造一个?是否只是创建一个常规应用程序(可能是控制台应用程序,因为没有 gui?)并以特殊方式运行或安装它,还是需要做更多的工作?
回答by JaredPar
There are a couple of things that jump out to me immediately.
有几件事让我立即想到。
- They run in an entirely different console starting with Vista
- As a result of running in a different console, services cannot interact with the desktop. So essentially there is no direct UI support. 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.
- 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.
- 从 Vista 开始,它们在完全不同的控制台中运行
- 由于在不同的控制台中运行,服务无法与桌面交互。所以基本上没有直接的 UI 支持。您通常必须编写一个同级 UI 应用程序,该应用程序作为普通程序运行并使用某种机制(例如命名管道)与服务进行通信。
- 通常,在任何给定时间只能运行一个服务实例。
- 进程是每个用户,服务是每个工作站,因此通常为多个用户提供服务。
回答by ShuggyCoUk
This MSDN pageleads to more documentation on creating them than you could shake a stick at. This pageis perhaps a better introduction to them in general.
这个 MSDN 页面提供了更多关于创建它们的文档,而不是你可以动摇的。一般而言,此页面可能是对它们的更好介绍。
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. 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). This reduces the effort at startup as multiple processes are not required for relatively lightweight services.
服务也不依赖于作为与进程的 1:1 映射运行。许多服务可以存在于一个进程中,通常是通过使用 svchost(使用进程浏览器查看这些服务以了解其工作原理)。这减少了启动时的工作量,因为相对轻量级的服务不需要多个进程。
Implementing a service in c# is pretty simple, this pageindicates 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 下的注册表中的脚手架定义了那些“图像路径”(在大多数情况下只是可执行文件和要使用的参数),它们被认为是服务然后以哪个用户身份运行,他们依赖哪些其他服务,以及他们是在启动/启动后还是根据需要启动。
回答by quanticle
If you're familiar with Unix, a Windows service is like a Unix daemon. It isn't associated with any particular user, and is always running in the background.
如果您熟悉 Unix,Windows 服务就像一个 Unix 守护进程。它不与任何特定用户相关联,并且始终在后台运行。
回答by soundslike
The main difference is that a windows service is something you want to run as a background service and doesn't require a UI. An example is a service that indexes files on your drive for searching.
主要区别在于 Windows 服务是您想要作为后台服务运行的东西,不需要 UI。一个示例是为您的驱动器上的文件编制索引以进行搜索的服务。
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. See the site below for an example: http://www.dotheweb.net/articles/dotnet/services.aspx
在 Visual Studio 中,您可以使用一种特殊的项目类型来创建它。有关示例,请参见以下站点:http: //www.dotheweb.net/articles/dotnet/services.aspx