运行WPF应用程序作为Windows服务 [英] Run a WPF Application as a Windows Service

查看:828
本文介绍了运行WPF应用程序作为Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好
我们正在开发,我们希望能够运行为Windows服务在Windows Presentation Foundation中的应用。

Hello We are developing a Windows Presentation Foundation Application that we would like to be able run as a Windows Service.

没有人做这样的事情?结果
是这可能吗?

Anyone done something like that?
Is it possible?

我们不要'吨需要与桌面或任何GUI交互,这纯粹是不错的一个应用程序,我们可以使用GUI运行,在命令行(即工作)或服务。

We don't need to interact with the Desktop or any GUI, it would just be nice to have one App that we can run with a GUI, from the Command Line (that works) or as a Service.

期待有趣输入: - )

Looking forward to interesting input :-)

推荐答案

随着经验的服务规则不应该有什么样的UI的,这是因为服务通常具有非常高的权限运行,可糟糕的事情都可能发生,如果你不是超级小心你的输入(我觉得窗户的最新版本不会让你做这件事的所有,但我不是100 %确定)。如果你需要与服务进行沟通,你应该使用某种形式的IPC(WCF,管道,插座,等...)的。如果你想要一个简单的控制台程序,也可以是我知道设置了一招的服务

As a rule of thumb services should never have any kind of UI, this is because services usually run with very high privileges and can bad things can happen if you are not super careful with your inputs (I think the newest versions of windows won't let you do it at all but I am not 100% sure). if you need to communicate with a service you should use some form of IPC (wcf, pipes, sockets, ect...). If you want a simple console program that also can be a service I know of a trick set that up.

class MyExampleApp : ServiceBase
{

    public static void Main(string[] args)
    {
        if (args.Length == 1 && args[0].Equals("--console"))
        {
            new MyExampleApp().ConsoleRun();
        }
        else
        {
            ServiceBase.Run(new MyExampleApp());
        }
    }
    private void ConsoleRun()
    {
        Console.WriteLine(string.Format("{0}::starting...", GetType().FullName));

        OnStart(null);

        Console.WriteLine(string.Format("{0}::ready (ENTER to exit)", GetType().FullName));
        Console.ReadLine();

        OnStop();

        Console.WriteLine(string.Format("{0}::stopped", GetType().FullName));
    }
    //snip
}

如果你刚开始该计划将推出服务(在骂你,如果你从控制台运行的话),但如果添加放慢参数 - 控制台在启动时,程序会启动并等待你按下回车键关闭。

if you just start the program it will launch as a service (and yell at you if you run it from the console) but if you add the paramter --console when you start it the program will launch and wait for you to hit enter to close.

这篇关于运行WPF应用程序作为Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆