简体   繁体   English

Windows Service Application仅执行一次长时间运行的任务,并在完成后自行停止

[英]Windows Service Application to perform long-running task only once and stop itself after completion

I am developing a Windows Service Application for sending bulk emails to the customers. 我正在开发Windows服务应用程序,用于向客户发送批量电子邮件。 In my case, I need to manually start and stop the service from the ASP.Net MVC web application passing some parameters. 就我而言,我需要通过一些参数从ASP.Net MVC Web应用程序手动启动和停止服务。 Since this is on demand and there is no schedule for this activity, I want the service to perform a long-running task (Sending bulk emails to 100K customers) only once, once started and then stop itself once completed. 由于这是按需提供的,而且没有此活动的时间表,因此我希望该服务仅执行一次长期任务(向10万个客户发送批量电子邮件),一旦启动,然后在完成后自行停止。

  1. Is this approach correct? 这种方法正确吗?
  2. If yes, how I could track its progress in MVC application? 如果是,如何在MVC应用程序中跟踪其进度?

Thank You, 谢谢,

This approach is incorrect. 这种方法是不正确的。 A "Windows Service" is a kind of application that waits for some client to call it, or an event to happen. “ Windows服务”是一种等待某些客户端调用它或事件发生的应用程序。 (It is called daemon in Unix/Linux jargon) (在Unix / Linux术语中称为守护程序)

In your case, "the service" should be an ordinary program that does batch processing. 在您的情况下,“服务”应该是执行批处理的普通程序。 So, just start a separate process with Process.Start(). 因此,只需使用Process.Start()启动一个单独的进程。 It doesn't block anything in your ASP.NET MVC application, it just does its job and terminate. 它不会阻止ASP.NET MVC应用程序中的任何内容,它只会执行其工作并终止。

There are two ways to track the progress. 有两种跟踪进度的方法。 You can call Process objects methods like WaitFor() with a timeout to see if it completes its job. 您可以使用超时调用诸如WaitFor()之类的Process对象方法,以查看其是否完成了工作。 Also, your "service" application would probably write its progress into a file, and the MVC application can read it anytime to see what's happening. 同样,您的“服务”应用程序可能会将其进度写入文件中,而MVC应用程序可以随时读取它以查看发生了什么。

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

相关问题 在Windows服务中运行长时间运行的任务 - Running a long-running Task within a Windows Service 从Windows服务处理长时间运行的操作 - Processing long-running operations from a windows service 关闭Windows服务中的长时间运行的进程 - Shutting down a Long-running process in a Windows Service Rx如何并行化长时间运行的任务? - Rx How to parallelize long-running task? 长期运行的后台任务中的Internet访问 - Internet access in a long-running background task 长期运行的WCF服务即将终止的原因是什么? - Reasons for long-running WCF service dying? 创建长期运行的工作流服务 - Creating a long-running Workflow Service 如何为长时间运行的作业停止Windows服务? - How to Stop a Windows Service for long running Jobs? 如何停止BackgroundWorker的DoWork处理程序仅包含一个(长时间运行)语句? - How to stop BackgroundWorker whose `DoWork` handler only contains one (long-running) statement? 在长期运行的Windows服务中进行依赖注入-组合根源是正确的主意吗? - Dependency injection in long-running Windows service - Composition root the right idea?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM