如何从Windows Service应用程序内部创建进程? [英] How to create a process internally from a Windows Service application?

查看:109
本文介绍了如何从Windows Service应用程序内部创建进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的Windows服务启动一个进程.但是,此过程是通过Windows Service单独创建的.因此,即使我停止该服务,该过程仍然会运行.我希望此过程可以在Windows Service中运行 我的当服务停止时,这也会自动终止该进程.我怎样才能做到这一点?下面是我的代码.

公共局部类Service1:ServiceBase
    {
        公共静态过程过程;
        静态线程th;
        公共服务1()
        {
            InitializeComponent();
        }

        受保护的重写void OnStart(string [] args)
        {
            
            th = new Thread(startMosquitto);
            th.Start();
            
        }

        受保护的重写void OnStop()
        {
            th.Join();
        }

        受静态保护的void startMosquitto()
        {
            process = new Process();
            process.StartInfo.FileName = @"mosquitto.exe";
            process.StartInfo.Arguments ="-c mosquitto.conf";
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.ErrorDialog = false;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            process.Start();
        }
    } 



mayooran99

解决方案

我不确定如果不移动整个mosquitto.exe代码,您打算做的事情是否可能插入服务中的备用线程.
您可以适当地修改服务以终止生成的进程,但这会很快变得混乱.

要真正为您提供帮助,我们需要知道该程序正在执行什么操作或实际的程序将要执行什么操作(假设mosquitto.exe只是一个占位符).


I am trying to launch a process from a Windows Service of mine. But this process is getting created separately, out of the Windows Service. Therefore even if I stop the service, the process still runs. I want this to process to run inside the Windows Service of mine. This will automatically kill the process when the service stops as well. How can I achieve this? Below is my code.

 public partial class Service1 : ServiceBase
    {
        public static Process process;
        static Thread th;
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            
            th = new Thread(startMosquitto);
            th.Start();
            
        }

        protected override void OnStop()
        {
            th.Join();
        }

        static protected void startMosquitto()
        {
            process = new Process();
            process.StartInfo.FileName = @"mosquitto.exe";
            process.StartInfo.Arguments = " -c mosquitto.conf";
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.ErrorDialog = false;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            process.Start();
        }
    }



mayooran99

解决方案

I am not sure what you are planning to do is possible without moving the entirety of the mosquitto.exe code into an alterante thread in the service.
You could propably modify the service to kill the spawned process on ending, but that could get messy quickly.

To really help you we would need to know what that programm is doing or what the real programm will be doing (asuming mosquitto.exe is just a placeholder).


这篇关于如何从Windows Service应用程序内部创建进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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