如何使用Windows服务运行EXE [英] How can I run an EXE using a windows service

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

问题描述

亲爱的所有人,

我已经设计了一个exe,现在我想使用Windows服务启动此EXE.其主要目的是每30分钟检查一次EXE是否正在运行或是否未使用该Windows服务.如果未运行,则Windows服务将启动它.

问候
Shahid Mehmood

Dear All,

I have already designed an exe and Now I want to launch this EXE using windows service. main purpose it to check after every 30 minutes either that EXE is running or not using that windows service. If not running then windows service will start it.

Regard''s
Shahid Mehmood

推荐答案

看看
Have a look on similar QA[^], check if you get some help out.


您可以使用BackgroundWorker进行线程处理,使用Process.WaitForExit()等待进程终止,直到停止服务为止. br/>
没错,应该做一些线程处理,在OnStart中进行大量工作可能会导致启动服务时无法从Windows正确启动的错误.
You can use a BackgroundWorker for the threading, use Process.WaitForExit() to wait for the process to terminate until you stop your service.

You''re right that you should do some threading, doing lots of work in the OnStart may render errors about not starting correctly from Windows when starting the service.
protected override void OnStart(string[] args) 
{ 
 
    BackgroundWorker bw = new BackgroundWorker(); 
    bw.DoWork += new DoWorkEventHandler(bw_DoWork); 
    bw.RunWorkerAsync(); 
} 
 
private void bw_DoWork(object sender, DoWorkEventArgs e) 
{ 
    Process p = new Process(); 
    p.StartInfo = new ProcessStartInfo("file.exe"); 
    p.Start(); 
    p.WaitForExit(); 
    base.Stop(); 
} 



编辑您可能还希望将Process p移至类成员,然后在OnStop中停止该进程,以确保如果exe出现问题,您可以再次停止该服务.



Edit You may also want to move the Process p to a class member and stop the process in the OnStop to make sure that you can stop the service again if the exe goes haywire.

protected override void OnStop() 
{ 
    p.Kill(); 
}


BackgroundWorker()未声明.那是什么.详细信息可在其中定义RunWorkerAsync()的位置.

仅需几行的任何建议就不需要知道任何可能的代码.如果可以理解的话,那么示例.
BackgroundWorker() is not declared. what is that. where is RunWorkerAsync() defined is detail avail.

any suggestion in just few lines required no Code is necessary just to know how it could be possible. if example then appreciated.


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

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