如果生成的线程仍在工作,则保持 Web 服务处于活动状态

发布于 2024-10-05 22:40:40 字数 699 浏览 0 评论 0原文

我有一个网络服务,将由夜间作业流程文档调用,每个文档将在其自己的后台处理器上排队并执行。此过程可能需要几个小时或几分钟,具体取决于负载。如果它什么都不做,我不想让它活着。但是,当生成一个线程并立即返回时,即使有一个线程正在工作,空闲时钟也会开始。我尚未将线程设置为 IsBackground,它仍然因空闲而终止。对于我的测试,我将空闲时间设置为 1 分钟。有没有办法让服务保持“活动”状态

这是网络服务代码:

public class LetterInitiateWS : System.Web.Services.WebService
    {

        private static Processor processor = new Processor();
        [WebMethod]
        public void ExecuteBulkRun()
        {
            var thrd = new Thread(() => ThreadExecuteBulkRun());
            thrd.IsBackground = false;
            thrd.Start();

        }

        private void ThreadExecuteBulkRun()
        {
            processor.ExecutBulkRun();
        }

    }

I have a webservice that will be called by a nightly job process documents, each document will be queued and executed on it's own background processor. This process can take a couple hours or a few munites depending on the load. I don't want to have it alive if it's not doing anything. But when a spawn a thread and return immediately the idle clock begins even though there is a thread working. i have not set the thread to IsBackground and it still terminates for being idle. For my test i set the idle time to be 1 minute. Is there a way to keep the service as being "ALIVE"

Here is the webservice code:

public class LetterInitiateWS : System.Web.Services.WebService
    {

        private static Processor processor = new Processor();
        [WebMethod]
        public void ExecuteBulkRun()
        {
            var thrd = new Thread(() => ThreadExecuteBulkRun());
            thrd.IsBackground = false;
            thrd.Start();

        }

        private void ThreadExecuteBulkRun()
        {
            processor.ExecutBulkRun();
        }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青萝楚歌 2024-10-12 22:40:40

我认为你最好的选择是将其分成两个单独的过程。您将在使用基于 IIS/ASP.NET 的进程并试图使其保持活动状态时遇到麻烦。

如果我是你,我会做类似的事情:

  1. 将记录写入数据库/文件/队列以发出长时间运行的线程必须启动的信号。
  2. 让 Windows 服务监视 DB/文件/队列中的信号,然后开始处理。

您将在此处的答案中找到类似的建议:

处理执行长时间运行的数据库任务时 Web 服务超时

长时间运行的 Web 服务架构

设计长期运行的资源密集型 Web 服务的建议

I think your best bet is to split this up into two seperate processes. You're going to have trouble with your IIS/ASP.NET based process and trying to keep it alive.

If I were you I'd look to do something like:

  1. Write a record into a DB/File/queue to signal the the long running thread must start.
  2. Have a windows service monitor the DB/file/queue for the signal and then begin the processing.

You'll find similar suggestions in the answers here:

Handling Web Service Timeouts While Performing Long-Running Database Tasks

Long running webservice architecture

Recommendations for designing a long-running, resource-intensive web service

怪异←思 2024-10-12 22:40:40

对于不涉及任何编码的解决方案,您可以通过将默认的空闲超时值 20 分钟更改为 0 来禁用 IIS 的不活动监视器。转到应用程序池中的高级设置,这是应用程序池中的第二个设置“过程模型”部分。

For a solution which doesn't involve any coding, you can just disable IIS' inactivity monitor by changing the default Idle Time-out value of 20 minutes to 0. Go to Advanced Settings in your application pool and this is the second setting in the "Process Model" section.

酷炫老祖宗 2024-10-12 22:40:40

为了处理这个问题,我创建了一个静态类,当我在 ThreadExecuteBulkRun 中工作时,该静态类有一个计时器,每 5 分钟记录一次 Web 服务页面,该页面会重置 IIS 中的空闲计时器。当该过程完成时,静态类将停止访问网页。

To handle this I created a static class that when I am working in ThreadExecuteBulkRun the static class has a timer that every 5 minutes hist the webservice page which resets the Idle timer in IIS. and when the process completes the static class stops hitting the webpage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文