创建工作线程,并保持它活在我的应用程序一生的时间来执行一些回地面任务 [英] to create a worker thread and keep it alive throughout my application life time to perform some back ground tasks

查看:101
本文介绍了创建工作线程,并保持它活在我的应用程序一生的时间来执行一些回地面任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个REQ凡在我要创建一个工作线程,并保持它活在我的应用程序一生的时间来执行一些回地面任务。那么有没有什么办法可以叠加的任务到此工作线程时我的应用程序曾经需要?

I have a req where in i have to create a worker thread and keep it alive throughout my application life time to perform some back ground tasks . so is there any way i can stack tasks to this worker thread when ever needed by my application .?

推荐答案

更新:即使你在评论中已经指出,你必须为此在Asp.Net,我会离开我的原下面的内容,因为它有一些有用的链接。

Update: Even though you've indicated in comments you have to do this in Asp.Net, I'll leave my original content below, as it has some useful links.

由于Asp.Net使用线程池来安排传入的请求,线程池运行的后台任务将需要一个线程关闭它,并会影响Asp.Net性能。因此,你将不得不使用类。

Since Asp.Net uses the thread pool to schedule incoming requests, running your background task on the thread pool will take one thread off of it and will impact Asp.Net performance. Thus, you will have to use the Thread class.

要实现你的场景中,你可以创建一个新的实例,其的IsBackground 属性设置为true,并开始它。一旦启动,该线程将等待<一个href=\"http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent.aspx\"><$c$c>AutoResetEvent (使用<$c$c>WaitOne法)由传入的请求进行设置(使用<$c$c>Set met6hod),其将信号后台线程,其任务应处理。一旦任务完成后,后台线程将再次对事件等待。

To achieve your scenario, you can create a new Thread instance, set its IsBackground property to true and start it. Once started, the thread will wait for an AutoResetEvent (using the WaitOne method) to be set by an incoming request (using the Set met6hod), which will signal the background thread that its task should be processed. Once the task is finished, the background thread will again wait on the event.

这是最简单的实现,即不允许该请求和后台线程之间传递参数,并且不允许在一个时间被排队多个任务。如果你需要的参数或排队支持,你将不得不继续给线程对象的引用某个地方,这病是进入的请求进行访问。

This is the simplest implementation, which does not allow passing parameters between the request and the background thread and does not allow more than one tasks to be queued at a time. If you need support for parameters or queueing, you will have to keep a reference to the thread object somewhere it ill be accessible to the incoming requests.

您还必须考虑到,你的后台线程可以在任何时候被杀死,如果IIS决定回收Asp.Net工作进程。此外,投掷后台线程内的异常将导致IIS以回收Asp.Net工作进程。

You will also have to consider that your background thread can be killed at any point in time, if IIS decides to recycle the Asp.Net worker process. Also, throwing an exception inside the background thread will cause IIS to recycle the Asp.Net worker process.

也有周围的后台线程的身份的一些注意事项。特别是,在后台线程不能轻易模拟对当前输入的请求的用户的身份。这是可能的,但它会需要以每一个新的任务是由一个请求预定的时间传递用户身份

There are also some considerations around the identity of the background thread. In particular, a background thread can't easily impersonate the identity of the user on the current incoming request. It is possible, but it will require you to pass the user identity each time a new task is scheduled by a request.


如果你告诉我们,这将是非常有用的信息语言和平台,你写你的$ C $℃的

It would be useful if you tell us what language and what platform you are writing your code in.

如果碰巧是Windows平台上,有一个线程池,你可以从你的任务借线程。您可以通过使用该<一个安排你的线程池任务href=\"http://msdn.microsoft.com/en-us/library/ms684957%28VS.85%29.aspx\"><$c$c>QueueUserWorkItem API(C ++)或<一个href=\"http://msdn.microsoft.com/en-us/library/system.threading.threadpool.queueuserworkitem.aspx\"><$c$c>ThreadPool.QueueUserWorkItem (C#/。NET)。注意有一些影响的,如果你的任务将运行较长时间。

If it happens to be a Windows platform, there is a thread pool you can "borrow" threads from for your tasks. You can schedule your task on the thread pool by using either the QueueUserWorkItem API (C++) or the ThreadPool.QueueUserWorkItem (C#/.Net). Note there are some implications if your task will be running for a longer time.

您也可以使用任一 主题创建您自己的线程 类(C#/。NET)或<一个href=\"http://msdn.microsoft.com/en-us/library/kdzttdcb%28VS.80%29.aspx\"><$c$c>_beginthreadex或<一个href=\"http://msdn.microsoft.com/en-us/library/ms682453%28VS.85%29.aspx\"><$c$c>CreateThread API(C ++)。在这种情况下,你将必须实现一个队列为前台线程调度的任务,你将有后台线程循环来挑选新的任务和执行它们。当然,你将不得不使用一些同步原语像<一个访问从两个线程队列同步href=\"http://msdn.microsoft.com/en-us/library/ms682530%28VS.85%29.aspx\"><$c$c>CRITICAL_SECTION (C ++)或 lock语句 (C#/。NET)。

You can also create your own thread using either the Thread class (C#/.Net) or the _beginthreadex or the CreateThread API (C++). In this case, you will have to implement a queue for the foreground thread to schedule the tasks on and you will have a loop on the background thread to pick the new tasks and execute them. And of course, you will have to synchronize the access to that queue from both threads using some synchronization primitive like a CRITICAL_SECTION (C++) or the lock statement (C#/.Net).

有关Linux或OS X,你可能会考虑 POSIX线程 。我没有做过太多的* nix的编程风格,所以有可能是更好的选择。如果你的目标的其中一个平台,这些信息添加到您的问题,我相信会有没有时间有用的答案。

For Linux or OS X you might look into POSIX threads. I have not done much *nix style programming, so there might be even better alternatives. If you are targeting one of these platforms, add that info to your question and I am sure there will be helpful answers in no time.

这篇关于创建工作线程,并保持它活在我的应用程序一生的时间来执行一些回地面任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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