asp.net 全局同步锁对象
ASP.NET 中有没有一种方法可以确保某个线程子程序无论如何都不会同时运行两次?
我现在拥有的代码是,
Public Class CheckClass
ReadOnly Property CheckSessionsLock As Object
Get
If HttpRuntime.Cache("CheckSessionsLock") Is Nothing Then HttpRuntime.Cache("CheckSessionsLock") = New Object
Return HttpRuntime.Cache("CheckSessionsLock")
End Get
End Property
Sub TryThreads()
Dim thread = New Thread(AddressOf TryLock)
thread.Priority = ThreadPriority.Lowest
thread.Start()
End Sub
Sub TryLock()
SyncLock CheckSessionsLock
DoTrace("entered locker")
For x = 0 To 10000
Next
DoTrace("exiting locker")
End SyncLock
DoTrace("exited locker")
End Sub
End Class
如果我在每个页面上运行此代码,那么代码会重叠几次。代码中的 DoTrace 函数只是将消息写入表中。
表中的消息应该一次又一次地按顺序出现(输入、退出、退出),但实际上并非如此。我就像进入、退出、进入、退出、退出......
这意味着同步锁不完整。是这样吗?
如果是这样,我们如何在代码块上跨请求和跨会话实现完整的同步锁?
编辑:我需要这个锁,因为真正的代码将根据数据库中的邮件类型列表发送电子邮件。发送每种邮件类型后,对其进行标记,然后继续下一个邮件。我不能在处理过程中,另一个线程应该将此邮件视为未处理。
请指教
is there a way in asp.net to make sure that a certain threaded sub is not run twice concurrently, no matter what?
the code i have now is
Public Class CheckClass
ReadOnly Property CheckSessionsLock As Object
Get
If HttpRuntime.Cache("CheckSessionsLock") Is Nothing Then HttpRuntime.Cache("CheckSessionsLock") = New Object
Return HttpRuntime.Cache("CheckSessionsLock")
End Get
End Property
Sub TryThreads()
Dim thread = New Thread(AddressOf TryLock)
thread.Priority = ThreadPriority.Lowest
thread.Start()
End Sub
Sub TryLock()
SyncLock CheckSessionsLock
DoTrace("entered locker")
For x = 0 To 10000
Next
DoTrace("exiting locker")
End SyncLock
DoTrace("exited locker")
End Sub
End Class
if i run this code on every page then several times the code overlaps. the DoTrace function in the code simply writes the message to a table.
the messages in the table should appear in order (entered,exiting,exited) again and again, but in reality, they don't. i get like entered, exiting,entered,exited,exiting...
this means that the synclock is not complete. is that true?
if so, how can we implement a complete synclock on a block of code, across requests and across sessions?
EDIT: i need this lock, as the real code will be sending emails, according to a list of mailing types in a db. after each mailing type is sent, its marked, then it continues with the next mailing. i cant have in middle of processing, another thread should see this mailing as unprocessed.
please advise
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否考虑过使用静态变量而不是使用 HttpRuntime Cache?
正如注释(可能有助于解释为什么您需要此功能),如果一次只能运行一次,您的网站将不会具有很好的可扩展性。
Rather than using the HttpRuntime Cache have you considered using a static variable?
Just as a note (it might be helpful to explain why you want this functionality) your website is not going to be very scalable if this can only be run once at a time.
在 C# 中(抱歉,不知道 VB 语法)我使用这个:
使用您的属性/缓存版本,您可能有两个线程尝试获取锁对象,并且每个线程创建一个不同的对象:
In C# (sorry, don't know VB syntax) I use this:
With your property/cache version, you could have two threads trying to get the lock-object and each creating a different one: