在Web服务中仅运行一个永远不会终止的作业/线程/进程(asp.net) [英] Running exactly one job/thread/process in webservice that will never get terminated (asp.net)

查看:72
本文介绍了在Web服务中仅运行一个永远不会终止的作业/线程/进程(asp.net)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好...

请考虑以下情况:

1-我有一个Web服务应用程序. Web服务接受作业.
2-作业存储在表中.
3-应该有一个进程/一个线程将一个接一个地处理作业.

以下是我的操作方法:

1-发出求职通知
2-Web服务将作业插入队列中
3- Web服务将检查作业线程,如果不存在,则会创建它.
4-线程进入无限循环,休眠10秒钟,然后再次检查是否有任何作业.如果是这样,请处理该作业,然后将其从表中删除.

问题是:

当用户会话终止时,作业线程终止.我想在Web服务中创建一个永远不会终止的线程/进程.

有什么主意吗?

Hi there...

Consider the following situation:

1- I have a webservice application. The webservice accept jobs.
2- The jobs are stored into a table.
3- There should be one process/one thread that will process the jobs one by one.

the following is how did I do it:

1- a call for a job is placed
2- the webservice insert the job into the queue
3- the webservice checks the job-thread and if it is not there, it creates it.
4- the thread enters an infinite loop, it sleeps for 10 seconds, and then checks again if there is any job. If so, process the job, and remove it from the table.

the problem is:

when the session of the user terminates , the job-thread is terminated. I want to create a thread/process in the webservice that will never terminate.

any idea?

thanks.

推荐答案

是的.我为您提供完整的解决方案.

您应该永远运行线程,但要保持等待状态,直到唤醒要执行的工作为止.
进入这种等待状态的方法是使用System.Threading.EvenWaitHandle.WaitOne.在调用此方法时,操作系统将关闭线程,并且从不调度线程直到唤醒,因此它消耗的CPU时间为零.可以通过以下另一个线程中的调用来唤醒该线程:在EvenWaitHandle的同一实例上的System.Threading.Thread.AbortSystem.Threading.EvenWaitHandle.Set,应在任务准备就绪时完成.如果您的Wait是用超时调用的,则线程也可以通过超时到期来唤醒.

但是您不必手动对此进行编程.您需要使用阻止队列.队列中的元素可以是任务或委托的数据集(请参阅下面的参考资料).

我在提示/技巧"文章中提供了一个通用阻止队列:简单线程通信和线程间调用的阻塞队列 [ ^ ].它包含完整的源代码和用法示例(其中一些已准备就绪,可以为您提供服务).另外,请注意同一页面上的替代方法:v.4.0中有一个Framework类在做相同的事情.不过,您需要遵循示例中显示的用法.

—SA
Yes. I have a complete solution for you.

You should run you thread forever but keep in in wait state until it is awaken for a job to do.
The way to get to such a wait state is using System.Threading.EvenWaitHandle.WaitOne. At the call to this method, your thread is switched off by OS and never scheduled back until awaken, so it consumes zero CPU time. The thread can be awaken by a call in the other thread: System.Threading.Thread.Abort or System.Threading.EvenWaitHandle.Set on the same instance of EvenWaitHandle, that should be done when the task is ready. If your Wait was called with timeout, the thread can be awaken by the expiration of the timeout as well.

But you don''t have to program this manually. You need to use a blocking queue. The elements of the queue could be data sets for you tasks or even delegates (see me reference below).

I provide a generic blocking queue in my Tips/Tricks article: Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^]. It is complete with full source code and usage samples (some are nearly ready for your service). Also, pay attention to the alternative on the same page: there is a Framework class doing the same thing in v.4.0. You need to follow the usage I''ve shown in my samples, though.

—SA


这篇关于在Web服务中仅运行一个永远不会终止的作业/线程/进程(asp.net)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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