如何让我的WPF应用程序在C#中作为服务运行?

时间:2022-06-22 20:56:49

I have a WPF application and I want to have it run as a service how do i do it in C#?

我有一个WPF应用程序,我希望它作为服务运行我如何在C#中执行?

2 个解决方案

#1


6  

EDIT (Based on comment)

编辑(根据评论)

If you want to make the program run at startup the easiest would be to change the setup to create a shortcut of the application output (exe) in the below directories:

如果要使程序在启动时运行,最简单的方法是更改​​设置以在以下目录中创建应用程序输出(exe)的快捷方式:

WIN 7: C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
XP: C:\Documents and Settings\All Users\Start Menu\Programs\Startup

WIN 7:C:\ ProgramData \ Microsoft \ Windows \ Start Menu \ Programs \ Startup XP:C:\ Documents and Settings \ All Users \ Start Menu \ Programs \ Startup

Original explanation

原始解释

Well given that a WPF application is a UI based application and a windows service is not, you would have to take out all of the interaction with the user from the application and then basically recreate it as a service.

鉴于WPF应用程序是基于UI的应用程序而Windows服务不是,您必须从应用程序中取出与用户的所有交互,然后基本上将其重新创建为服务。

There is no easy 1 step process to do this given that they are made to do 2 completely different things.

没有简单的一步过程可以做到这一点,因为它们可以做两件完全不同的事情。

A service sits in the background and performs tasks that do not recuire user intput, and in most cases does not interact with the user except for a config file; Even though you can run the service to interact with the desktop but that works completely different from a normal wpf program.

服务位于后台并执行不会导致用户输入的任务,并且在大多数情况下除了配置文件之外不与用户交互;即使您可以运行该服务与桌面交互,但它与普通的wpf程序完全不同。

A Wpf program on the under hand (as the presentation part of the name suggests) is made to interact with the user.

手头的Wpf程序(如名称的表示部分所示)用于与用户交互。

Why would you want to run it as a service? The whole point of a service is to run in the background with minimum user interaction.

为什么要将其作为服务运行?服务的重点是在后台运行,用户交互最少。

#2


1  

Divide your app into two parts;

将您的应用分为两部分;

  1. A Windows service app that runs in the background (possibly auto runs at dtartup.and runs on system account).

    在后台运行的Windows服务应用程序(可能在dtartup上自动运行并在系统帐户上运行)。

  2. A user interaction app that communicates with.and controls the windows service.

    与之通信并控制Windows服务的用户交互应用程序。

Consider WCF to communicate between the two.

考虑WCF在两者之间进行通信。

Read about SOA (Service Oriented Architecture) if you want to better understand this.

如果您想更好地理解这一点,请阅读SOA(面向服务的体系结构)。

#1


6  

EDIT (Based on comment)

编辑(根据评论)

If you want to make the program run at startup the easiest would be to change the setup to create a shortcut of the application output (exe) in the below directories:

如果要使程序在启动时运行,最简单的方法是更改​​设置以在以下目录中创建应用程序输出(exe)的快捷方式:

WIN 7: C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
XP: C:\Documents and Settings\All Users\Start Menu\Programs\Startup

WIN 7:C:\ ProgramData \ Microsoft \ Windows \ Start Menu \ Programs \ Startup XP:C:\ Documents and Settings \ All Users \ Start Menu \ Programs \ Startup

Original explanation

原始解释

Well given that a WPF application is a UI based application and a windows service is not, you would have to take out all of the interaction with the user from the application and then basically recreate it as a service.

鉴于WPF应用程序是基于UI的应用程序而Windows服务不是,您必须从应用程序中取出与用户的所有交互,然后基本上将其重新创建为服务。

There is no easy 1 step process to do this given that they are made to do 2 completely different things.

没有简单的一步过程可以做到这一点,因为它们可以做两件完全不同的事情。

A service sits in the background and performs tasks that do not recuire user intput, and in most cases does not interact with the user except for a config file; Even though you can run the service to interact with the desktop but that works completely different from a normal wpf program.

服务位于后台并执行不会导致用户输入的任务,并且在大多数情况下除了配置文件之外不与用户交互;即使您可以运行该服务与桌面交互,但它与普通的wpf程序完全不同。

A Wpf program on the under hand (as the presentation part of the name suggests) is made to interact with the user.

手头的Wpf程序(如名称的表示部分所示)用于与用户交互。

Why would you want to run it as a service? The whole point of a service is to run in the background with minimum user interaction.

为什么要将其作为服务运行?服务的重点是在后台运行,用户交互最少。

#2


1  

Divide your app into two parts;

将您的应用分为两部分;

  1. A Windows service app that runs in the background (possibly auto runs at dtartup.and runs on system account).

    在后台运行的Windows服务应用程序(可能在dtartup上自动运行并在系统帐户上运行)。

  2. A user interaction app that communicates with.and controls the windows service.

    与之通信并控制Windows服务的用户交互应用程序。

Consider WCF to communicate between the two.

考虑WCF在两者之间进行通信。

Read about SOA (Service Oriented Architecture) if you want to better understand this.

如果您想更好地理解这一点,请阅读SOA(面向服务的体系结构)。