asp.net 全局同步锁对象

发布于 2024-10-15 23:56:25 字数 1045 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久光 2024-10-22 23:56:26

您是否考虑过使用静态变量而不是使用 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.

梦一生花开无言 2024-10-22 23:56:26

在 C# 中(抱歉,不知道 VB 语法)我使用这个:

private static readonly object Padlock = new object();
  • 它是一个字段,而不是属性,
  • 它是静态的(在 VB 中,如果我没记错的话,它是“共享”),所以它在整个应用程序中都是相同
  • 的一旦您使用此类,而不是在您显式使用该字段时,就初始化一次。

使用您的属性/缓存版本,您可能有两个线程尝试获取锁对象,并且每个线程创建一个不同的对象:

  • 线程 1 检查缓存,但没有找到对象
  • 线程 1 已停放
  • 线程 2 检查缓存,没有找到该对象t 找到对象
  • 线程 2 创建该对象并缓存它,再次检索它并从属性返回
  • 线程 1 恢复
  • 线程 1 创建一个新对象并缓存它,再次检索它并返回与线程 2 使用的不同的锁对象
  • 任何其他线程将使用线程1的锁对象

In C# (sorry, don't know VB syntax) I use this:

private static readonly object Padlock = new object();
  • It's a field, not a property,
  • It's static (in VB, that's "shared" if I'm not mistaken) so it's the same throughout the entire application
  • It's initialised once as soon as you use this class, not when you explicitly use the field.

With your property/cache version, you could have two threads trying to get the lock-object and each creating a different one:

  • Thread 1 checks the cache and doesn't find the object
  • Thread 1 is parked
  • Thread 2 checks the cache, doesn't find the object
  • Thread 2 creates the object and caches it, retrieves it again and returns from the property
  • Thread 1 resumes
  • Thread 1 creates a new object and caches it, retrieves it again and returns a different lock object than thread 2 uses
  • Any further threads will use the lock object of thread 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文