如何使用Windows服务运行EXE

编程入门 行业动态 更新时间:2024-10-20 15:51:39
本文介绍了如何使用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

推荐答案

看看类似的质量检查 [ ^ 时,从Windows服务运行并停止服务],检查是否有帮助. 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

本文发布于:2023-11-05 08:16:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560333.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   Windows   EXE

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!