单独的服务线程 [英] seperate service thread

查看:68
本文介绍了单独的服务线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的asp.net应用程序中创建一个子系统负责

用于需要根据特定事件发送出去的电子邮件,以便

主要请求/响应线程不负责与

smtp服务器等的通信。我不想为每个创建一个单独的线程

必须发送通知的实例...我宁愿将这些必要的信息记录到数据库或网络缓存中,并让子系统为

负责它从那里。最好的方法是什么呢?这个....我应该在应用程序启动时启动另一个线程吗?

当我没有责任时我会做什么。它应该循环,看看

的工作和几天的工作/睡眠等等?


我肯定有人做过类似的事情这之前或遇到一些

的文章等...我很感激任何评论。


~Paul

I''d like to create a subsystem in my asp.net application that is responsible
for emails that need to be send out based upon certain events so that the
main request/response threads aren''t responsible for the communication with
the smtp server, etc. I don''t want to create a seperate thread for every
instance in which a notification must be sent...I''d rather record the
necessary information to the db or web cache and let the subsystem be
responsible for it from there. What is the best way to go about
this....should I just launch another thread upon application start? What do
I do w/ the thread when it has no responsibility. Should it just loop, look
for work and do work/sleep for a few seconds, etc?

I''m sure someone has done something like this before or come across some
article, etc...I appreciate any comments.

~Paul

推荐答案

我建​​议您考虑使用Windows服务来处理这样的背景

任务。


这里有更多有关Windows服务的信息:
http://msdn.microsoft.com/library/de...owsService.asp


-

我希望这有帮助,

Steve C. Orr,MCSD,MVP
http://Steve.Orr.net


" PJ" < PJ *** @ hotmail.com>在留言中写道

news:uE ************* @ TK2MSFTNGP09.phx.gbl ...
I suggest you look into using a Windows Service to handle such background
tasks.

Here''s more information on Windows Services:
http://msdn.microsoft.com/library/de...owsService.asp

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net

"PJ" <pj***@hotmail.com> wrote in message
news:uE*************@TK2MSFTNGP09.phx.gbl...
我想要在我的asp.net应用程序中创建一个子系统,负责根据某些事件发送出去的电子邮件,以便主要请求/响应线程不负责与smtp服务器等沟通
我不想为每个必须发送通知的实例创建一个单独的线程...我宁愿将必要的信息记录到数据库或Web缓存中,让子系统从那里负责。什么是最好的方式
这....我应该在应用程序启动时启动另一个线程?什么
做什么
我没有责任的线程。它应该循环,
看工作,工作/睡几秒钟等等吗?

我相信有人之前做过类似的事情或来过在一些
文章等...我感谢任何评论。

~Paul
I''d like to create a subsystem in my asp.net application that is
responsible
for emails that need to be send out based upon certain events so that the
main request/response threads aren''t responsible for the communication
with
the smtp server, etc. I don''t want to create a seperate thread for every
instance in which a notification must be sent...I''d rather record the
necessary information to the db or web cache and let the subsystem be
responsible for it from there. What is the best way to go about
this....should I just launch another thread upon application start? What
do
I do w/ the thread when it has no responsibility. Should it just loop,
look
for work and do work/sleep for a few seconds, etc?

I''m sure someone has done something like this before or come across some
article, etc...I appreciate any comments.

~Paul





" PJ" < PJ *** @ hotmail.com>在留言中写道

news:uE ************* @ TK2MSFTNGP09.phx.gbl ...

"PJ" <pj***@hotmail.com> wrote in message
news:uE*************@TK2MSFTNGP09.phx.gbl...
我想要在我的asp.net应用程序中创建一个子系统,该子系统是
,负责根据某些事件发送出去的电子邮件,以便主要请求/响应线程不负责通信
与smtp服务器等。我不想为每个必须发送通知的实例创建一个单独的线程...我宁愿记录
必要的信息到数据库或Web缓存,让子系统从那里负责。什么是最好的方式
这....我应该在应用程序启动时启动另一个线程?当我没有责任时,我会用线程做什么
。它应该只是循环,
寻找工作,并且工作/睡眠几秒钟等等?

我确定有人之前做过类似的事情或遇到过某些事情/>文章等...我感谢任何评论。
I''d like to create a subsystem in my asp.net application that is responsible for emails that need to be send out based upon certain events so that the
main request/response threads aren''t responsible for the communication with the smtp server, etc. I don''t want to create a seperate thread for every
instance in which a notification must be sent...I''d rather record the
necessary information to the db or web cache and let the subsystem be
responsible for it from there. What is the best way to go about
this....should I just launch another thread upon application start? What do I do w/ the thread when it has no responsibility. Should it just loop, look for work and do work/sleep for a few seconds, etc?

I''m sure someone has done something like this before or come across some
article, etc...I appreciate any comments.



这是一种名为SingleThreadWorkQueue的类型。当你创建一个SingleThreadWorkQueue时,创建一个线程和一个Queue。线程

将项目从队列中拉出,然后调用一个委托(类型外的函数
),并将其传递给队列中的任何内容。


因此,您只需捆绑信息以将电子邮件发送到对象,

并将其传递给SingleThreadWorkQueue.AddWork。工作线程然后将b / b
离开工作并调用你的工作函数。


像这样:


- - 全球范围内的某个地方----

公共共享OutgoingEmailQueue作为新的SingleThreadWorkQueue(地址如此

SendAnEmail)


公共共享sub SendAnEmail(作为对象工作)

dim em as MyEmailInfo = directcast(work,MyEmailInfo)

- 发送电子邮件

end sub

----你想在哪里生成一封电子邮件,有点像----

MyGlobal.OutgoingEmailQueue.AddWork(新的EmailObject(" jo *) @ hotmail.com"," helo,

joe"))

私有类SingleThreadWorkQueue

私有workQueue As Queue = Queue.Synchronized(New Queue())

Private wp As DoWork

Private wt As Thread

Public Delegate Sub DoWork(ByVal w As Object )

公共事件ExceptionOccured(ByVal e As Exception)


Public Sub新的(ByVal WorkProc作为DoWork)

wp = WorkProc

wt =新线程(AddressOf workerThreadProc)

wt.IsBackground = True
wt.Name =" worker thread for" &安培; wp.​​Target.GetType.Name& "。" &

wp.Method.Name

wt.Start()

End Sub


Private Sub workerThreadProc()

SyncLock workQueue

Do

Monitor.Wait(workQueue,5000)

Do While workQueue.Count> 0

尝试

Dim w As Object = workQueue.Dequeue

wp.Invoke(w)

Catch ex作为例外

RaiseEvent ExceptionOccured(ex)

结束尝试

循环

循环

结束SyncLock

结束子

Public Sub AddWork(ByVal作为对象)

workQueue.Enqueue(work)

如果Monitor.TryEnter(workQueue)那么

Monitor.Pulse(workQueue)

Monitor.Exit(workQueue)

结束如果

结束子


结束类


Here''s a type called SingleThreadWorkQueue for this kind of thing. When you
create a SingleThreadWorkQueue is creates a thread and a Queue. The thread
pulls items off the queue and then invokes a delegate (a function outside
the type), and passes it whatever was on the queue.

So you would just bundle up the information to send an email into an object,
and pass it to SingleThreadWorkQueue.AddWork. The worker thread would then
dequeue the work and invoke your work function.

Like this:

--- somewhere in global scope ----
public shared OutgoingEmailQueue as new SingleThreadWorkQueue(addressOf
SendAnEmail)

public shared sub SendAnEmail(work as Object)
dim em as MyEmailInfo = directcast(work,MyEmailInfo)
--send the email
end sub
---- anywhere you want to generate an email, somehting like ----

MyGlobal.OutgoingEmailQueue.AddWork(new EmailObject("jo*@hotmail.com","helo,
joe"))

Private Class SingleThreadWorkQueue
Private workQueue As Queue = Queue.Synchronized(New Queue())
Private wp As DoWork
Private wt As Thread
Public Delegate Sub DoWork(ByVal w As Object)
Public Event ExceptionOccured(ByVal e As Exception)

Public Sub New(ByVal WorkProc As DoWork)
wp = WorkProc
wt = New Thread(AddressOf workerThreadProc)
wt.IsBackground = True
wt.Name = "worker thread for " & wp.Target.GetType.Name & "." &
wp.Method.Name
wt.Start()
End Sub

Private Sub workerThreadProc()
SyncLock workQueue
Do
Monitor.Wait(workQueue, 5000)
Do While workQueue.Count > 0
Try
Dim w As Object = workQueue.Dequeue
wp.Invoke(w)
Catch ex As Exception
RaiseEvent ExceptionOccured(ex)
End Try
Loop
Loop
End SyncLock
End Sub
Public Sub AddWork(ByVal work As Object)
workQueue.Enqueue(work)
If Monitor.TryEnter(workQueue) Then
Monitor.Pulse(workQueue)
Monitor.Exit(workQueue)
End If
End Sub

End Class


这在某些情况下可行,但如果线程特别长时间运行

我担心一旦启动线程(ASP.NET页面)

早已消失,它可能会超时。 Windows服务永远不会超时,所以它可能会更加可靠。一个队列仍然可以用来存储待办事项列表。


-

我希望这会有所帮助,

Steve C. Orr,MCSD,MVP
http://Steve.Orr.net

" David Browne" < davidbaxterbrowne没有盆栽 me**@hotmail.com >写在

消息新闻:uM ************** @ TK2MSFTNGP10.phx.gbl ...
This could work in some cases, but if the thread is especially long running
I''m afraid it could time out once the initiating thread (the ASP.NET page)
is long gone. A windows service should never time out, so it might be a bit
more reliable. A Queue could still be used to store up the list of to-dos.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:uM**************@TK2MSFTNGP10.phx.gbl...

" ; PJ" < PJ *** @ hotmail.com>在消息中写道
新闻:uE ************* @ TK2MSFTNGP09.phx.gbl ...

"PJ" <pj***@hotmail.com> wrote in message
news:uE*************@TK2MSFTNGP09.phx.gbl...
我想创建一个子系统我的asp.net应用程序
I''d like to create a subsystem in my asp.net application that is


负责

用于需要根据特定事件发送出去的电子邮件,以便
主要请求/响应线程不是'使用
for emails that need to be send out based upon certain events so that the
main request/response threads aren''t responsible for the communication
smtp服务器等来负责通信


with


我不想为每个
实例创建一个单独的线程。必须发送通知...我宁愿将必要的信息记录到数据库或网络缓存中,让子系统从那里负责。什么是最好的方式
这....我应该在应用程序启动时启动另一个线程?什么

the smtp server, etc. I don''t want to create a seperate thread for every
instance in which a notification must be sent...I''d rather record the
necessary information to the db or web cache and let the subsystem be
responsible for it from there. What is the best way to go about
this....should I just launch another thread upon application start? What


我没有责任的话。它应该循环,
I do w/ the thread when it has no responsibility. Should it just loop,


看看

工作并做几个小时的工作/睡眠等等?

我肯定有人我之前做过类似的事情或者遇到过一些文章等...我很欣赏任何评论。
for work and do work/sleep for a few seconds, etc?

I''m sure someone has done something like this before or come across some
article, etc...I appreciate any comments.



这是一种名为SingleThreadWorkQueue的类型。当你创建一个SingleThreadWorkQueue时,会创建一个线程和一个Queue。
线程将项目从队列中拉出然后调用一个委托(类型外的一个函数),并将其传递给队列中的任何内容。

所以您只需捆绑信息以将电子邮件发送到
对象,然后将其传递给SingleThreadWorkQueue.AddWork。工作线程将
然后取消工作并调用您的工作职能。

这样:

---全球范围内的某个地方 - -
公共共享OutgoingEmailQueue作为新SingleThreadWorkQueue(addressOf
SendAnEmail)

公共共享子SendAnEmail(工作为对象)
暗淡EM作为MyEmailInfo = directcast(工作, MyEmailInfo)
- 发送电子邮件
结束子

----你想在哪里发电子邮件,有点像----
MyGlobal.OutgoingEmailQueue.AddWork(新的
EmailObject(" jo *@hotmail.com",helo,
joe))


私人班级SingleThreadWorkQueue
私有workQueue As Queue = Queue.Synchronized(新队列())
私有wp作为DoWork
私有wt作为线程
Public Delegate Sub DoWork(ByVal w As Object)
公共事件ExceptionOccured(ByVal e As Exception)

Public Sub New(ByVal WorkProc As DoWork)
wp = WorkProc
wt =新线程(AddressOf workerThreadProc)
wt.IsBackground = True
wt.Name =" worker thread for &安培; wp.​​Target.GetType.Name& "。" &
wp.Method.Name
wt.Start()
End Sub

私有子workerThreadProc()
SyncLock workQueue

Monitor.Wait(workQueue,5000)
同时workQueue.Count> 0
尝试
Dim w As Object = workQueue.Dequeue
wp.Invoke(w)
Catch ex As Exception
RaiseEvent ExceptionOccured(ex)
End试试
Loop
循环
结束SyncLock
End Sub
Public Sub AddWork(ByVal work as Object)
workQueue.Enqueue(work)
如果Monitor.TryEnter(workQueue)那么<< Monitor.Pulse(workQueue)
Monitor.Exit(workQueue)
结束如果
结束子

结束类


Here''s a type called SingleThreadWorkQueue for this kind of thing. When
you
create a SingleThreadWorkQueue is creates a thread and a Queue. The
thread
pulls items off the queue and then invokes a delegate (a function outside
the type), and passes it whatever was on the queue.

So you would just bundle up the information to send an email into an
object,
and pass it to SingleThreadWorkQueue.AddWork. The worker thread would
then
dequeue the work and invoke your work function.

Like this:

--- somewhere in global scope ----
public shared OutgoingEmailQueue as new SingleThreadWorkQueue(addressOf
SendAnEmail)

public shared sub SendAnEmail(work as Object)
dim em as MyEmailInfo = directcast(work,MyEmailInfo)
--send the email
end sub
---- anywhere you want to generate an email, somehting like ----

MyGlobal.OutgoingEmailQueue.AddWork(new
EmailObject("jo*@hotmail.com","helo,
joe"))

Private Class SingleThreadWorkQueue
Private workQueue As Queue = Queue.Synchronized(New Queue())
Private wp As DoWork
Private wt As Thread
Public Delegate Sub DoWork(ByVal w As Object)
Public Event ExceptionOccured(ByVal e As Exception)

Public Sub New(ByVal WorkProc As DoWork)
wp = WorkProc
wt = New Thread(AddressOf workerThreadProc)
wt.IsBackground = True
wt.Name = "worker thread for " & wp.Target.GetType.Name & "." &
wp.Method.Name
wt.Start()
End Sub

Private Sub workerThreadProc()
SyncLock workQueue
Do
Monitor.Wait(workQueue, 5000)
Do While workQueue.Count > 0
Try
Dim w As Object = workQueue.Dequeue
wp.Invoke(w)
Catch ex As Exception
RaiseEvent ExceptionOccured(ex)
End Try
Loop
Loop
End SyncLock
End Sub
Public Sub AddWork(ByVal work As Object)
workQueue.Enqueue(work)
If Monitor.TryEnter(workQueue) Then
Monitor.Pulse(workQueue)
Monitor.Exit(workQueue)
End If
End Sub

End Class



这篇关于单独的服务线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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