asp.net全局同步锁对象 [英] asp.net global synclock object

查看:302
本文介绍了asp.net全局同步锁对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

asp.net中是否有一种方法可以确保某个线程子程序无论如何都不会同时运行两次?

is there a way in asp.net to make sure that a certain threaded sub is not run twice concurrently, no matter what?

我现在拥有的代码是

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函数只是将消息写到表中.

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?

编辑:我需要此锁,因为根据db中邮件类型的列表,实际代码将发送电子邮件.发送每种邮件类型后,将其标记,然后继续进行下一次邮件发送.我无法进行处理,另一个线程应该将此邮件视为未处理.

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.

请告知

推荐答案

您是否考虑过使用静态变量,而不是使用HttpRuntime缓存?

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.

这篇关于asp.net全局同步锁对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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