简体   繁体   English

如何在C#中创建可以与GUI *或*一起运行的Windows应用程序?

[英]How can I create a Windows application that can run with a GUI *or* as a Windows service in C#?

I'd like to create an application using C# that...: 我想使用C#创建一个应用程序...:

  • Can be run as a Windows application, with a GUI (it'll indicate progress, status, etc.) 可以使用GUI作为Windows应用程序运行(它将指示进度,状态等)

    OR 要么

  • Can be run as a Windows service, without a GUI 无需Windows即可作为Windows服务运行

Is it possible? 可能吗? Can someone get me started? 有人可以让我开始吗?

I guess the alternative is that I could create a Windows service, and then a separate GUI application which can poll the Windows service to fetch data from it (progress, status, etc.). 我猜这是我可以创建一个Windows服务,然后创建一个单独的GUI应用程序,该应用程序可以轮询Windows服务以从中获取数据(进度,状态等)。

In that case... how can I fetch data from the Windows service from my GUI application? 在这种情况下,如何从GUI应用程序从Windows服务中获取数据?

I'm doing something similar to what you're asking for. 我正在做的事情与您要的类似。 I have programmed it so that if you send the command line parameter "/form" to the executable, it will pop up a windows form instead of running as a service. 我对它进行了编程,以便将命令行参数“ / form”发送给可执行文件时,它将弹出一个Windows窗体,而不是作为服务运行。

As far as running the background job itself, in both cases you will need to do some sort of threading (perhaps with a timer) to do the work and report status back to your form asynchronously. 就运行后台作业本身而言,在两种情况下,您都需要执行某种线程处理(也许使用计时器)来完成工作并异步将状态报告给表单。 This would be a whole different discussion topic on creating threaded GUI apps. 关于创建线程GUI应用程序,这将是一个完全不同的讨论主题。

The "form or service" code goes something like this: “表单或服务”代码如下所示:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    private static void Main(string[] args)
    {
        if (args.Length > 0 && args[0] == "/form")
        {
            var form = new MainForm();
            Application.Run(form);
            return;
        }

        var ServicesToRun = new ServiceBase[]
            {
                new BackgroundService()
            };

        ServiceBase.Run(ServicesToRun);
    }
}

I've never done an app that can be run as a windows service or a GUI, but we have a lot of apps that you can switch between console app and windows service using a compiler flag. 我从未做过可以作为Windows服务或GUI运行的应用程序,但是我们有很多应用程序可以使用编译器标志在控制台应用程序和Windows服务之间进行切换。 (I just saw the answer with the cmd line arg - that might even be better!) (我刚刚看到了cmd行arg的答案-可能更好!)

We usually then just use a compiler flag to switch between the two. 然后,我们通常只使用编译器标志在两者之间进行切换。 Here's an example... I didn't completely think this through, but it might give you a start: 这是一个示例...我并未完全考虑到这一点,但这可能会为您提供一个起点:

#define RUN_AS_SERVICE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.ServiceProcess;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
#if RUN_AS_SERVICE

            System.ServiceProcess.ServiceBase[] ServicesToRun;
            ServicesToRun = new System.ServiceProcess.ServiceBase[]
            {
                new MyService()
            };
            System.ServiceProcess.ServiceBase.Run(ServicesToRun);
#else
            // Run as GUI
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
#endif
        }
    }
    public class MyService : ServiceBase
    {
        protected override void OnStart(string[] args)
        {
            // Start your service
        }

        protected override void OnStop()
        {
            // Stop your service
        }
    }
}

Build the actual application in libraries. 在库中构建实际的应用程序。 You can then add any UI (I say this loosely, as service is not really a UI) you desire. 然后,您可以添加所需的任何UI(由于服务不是真正的UI,我可以这样说)。 As long as you think of the app as a windows aplication and a service application, you are developing two applications. 只要您将该应用程序视为Windows应用程序和服务应用程序,就可以开发两个应用程序。 If you think of the application as the business problem it solves, you will then think of the windows forms UI and the service UI, which is much saner for your needs. 如果您将应用程序视为要解决的业务问题,那么您将想到Windows窗体UI和服务UI,这对于您的需求而言更为合理。

While this may sound sane, you would be surprised how many applications need a complete overwrite to become a different UI type. 尽管这听起来很理智,但您会惊讶于有多少应用程序需要完全覆盖才能成为其他UI类型。 thank you for the question. 感谢你的提问。 It convinces me there is a need for the book I am writing. 它使我确信我正在写这本书。 :-) :-)

You should go with the latter option. 您应该选择后者。 Create your service and then a seperate GUI app. 创建您的服务,然后创建一个单独的GUI应用程序。 Most of the plumbing for doing all this is already provided for you in the framework. 框架中已经为您提供了用于执行所有这些操作的大多数管道。 Have a look at the ServiceController class. 看一下ServiceController类。

I saw this thread, it might have some more info for you 我看到了这个主题,可能会为您提供更多信息

How to write c# service that I can also run as a winforms program? 如何编写也可以作为Winforms程序运行的C#服务?

暂无
暂无

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

相关问题 如何创建一个 windows 应用程序,该应用程序应该在.Net C# 中以管理员权限运行,而不会一次又一次地提示 UAC? - How can I create a windows application that should run with admin rights in .Net C# without prompting UAC again and again? 我可以在Windows服务中运行另一个命令提示符/ GUI进程吗? - Can I run another Command Prompt/GUI process in a Windows Service? C# Windows 控制台应用程序如何判断它是否以交互方式运行 - How can a C# Windows Console application tell if it is run interactively 我可以使用Application(GUI)和Windows Service创建一个项目,并使用一个安装程序安装两者吗? - Can I create one project with Application(GUI) and Windows Service and have one setup which installs both? 如何创建一个独立的exe应用程序的Windows服务? - How can I create a windows service that is a standalone exe application? C#windows应用程序可以在基于Windows 8的平板电脑上运行吗? - can a C# windows application be run on a windows 8 based tablet well? 如何使用 C# 从 Windows 服务运行 EXE 程序? - How can I run an EXE program from a Windows Service using C#? 如何从c#中的Windows服务应用程序中为所有用户清空回收站 - How can I empty the recycle bin for all users from a Windows service application in c# 如何在用C#编写的Windows服务中获取ServiceName? - How can I get the ServiceName in a Windows Service written in C#? 我如何在c#中等待一段时间(Windows服务) - How I can waiting some time in c# (windows service)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM