asp.net多线程与的SyncLock [英] asp.net multithreading with synclock
问题描述
我有一些测试code,我在页面的每一个负荷我的asp.net网站运行
i have some test code which i run at every load of a page in my asp.net website
这是code
Sub TryThreads()
Dim t1 = New Thread(AddressOf TryLock)
t1.Priority = ThreadPriority.Lowest
t1.Start()
Dim t2 = New Thread(AddressOf TryLock)
t2.Priority = ThreadPriority.Lowest
t2.Start()
End Sub
Sub TryLock()
Dim LockObject = New Object
SyncLock LockObject
DoTrace("entered locker")
For x = 0 To 10000
Next
DoTrace("exiting locker")
End SyncLock
DoTrace("exited locker")
End Sub
在dotrace简单地记录在数据库添加到日志表
the "dotrace" simply add a record to a log table in the db
现在正确的结果将是,我应该有,才能在数据库中的条目进入,退出,退出
now the right result would be that i should have the entries in the db in order "entered","exiting","exited"
但实际上,当我在看分贝我看到第2进入,然后2退出等。
这意味着多线程工作正常,但不是的SyncLock
是正确的?
but actually when i look in the db i see first 2 "entered" then 2 "exiting" etc. meaning that the multithreading is working ok, but not the synclock is that correct?
和如何这个问题能解决?
真正的code将增加记录数据库,并可能会从不同的会话的多个页面调用,但是同样的code不能同时运行两次
and how can this be fixed? the real code will be adding records to the db and might be called from several pages of different sessions, but the same code must not run twice concurrently
我做AP preciate任何人的帮助。
i do appreciate anybodys help
非常感谢你!
编辑:
针对Sashas精彩后我改变了我的code到A类(这是一个模块),现在它看起来是这样的:
in response to Sashas wonderful post i changed my code to a class (it was in a module) and now it looks like this:
Public Class CheckClass
Property LockObject As Object
Get
If HttpRuntime.Cache("CheckSessionsLock") Is Nothing Then HttpRuntime.Cache("CheckSessionsLock") = New Object
Return HttpRuntime.Cache("CheckSessionsLock")
End Get
Set(ByVal value As Object)
If value Is Nothing Then
HttpRuntime.Cache.Remove("CheckSessionsLock")
Else
HttpRuntime.Cache("CheckSessionsLock") = value
End If
End Set
End Property
Sub TryThreads()
Dim t1 = New Thread(AddressOf TryLock)
t1.Priority = ThreadPriority.Lowest
t1.Start()
Dim t2 = New Thread(AddressOf TryLock)
t2.Priority = ThreadPriority.Lowest
t2.Start()
End Sub
Sub TryLock()
SyncLock LockObject
DoTrace("entered locker")
For x = 0 To 10000
Next
DoTrace("exiting locker")
End SyncLock
DoTrace("exited locker")
End Sub
End Class
现在它工作的时候80-90%。
在页面加载我有:
now it works 80-90% of the time. on page load i have:
Dim cc = New CheckClass
cc.TryThreads()
如果我同时打开多个页面,他们仍然交锋一些时间。但如果我是正确的,这个问题目前不是用的SyncLock尽可能与httpruntime.cache,使用标准的属性时,在一个页面上,因为在code工程100%。
if i open multiple pages at once, they still clash some times. but if i'm correct, the issue is now not with the synclock as much as with the httpruntime.cache, because when using a standard property, on one page, the code works 100%.
那么,如何才能确保2个线程,甚至完全不同的会话不能同时运行的tryLock?
so how can i make sure that 2 threads, even from totally different sessions never run the trylock simultaneously?
谢谢大家帮忙
推荐答案
您正在创建一个新的对象实例时,的tryLock
方法被调用,并利用它来进行锁定。如果你想两个线程之间的相互排斥,你需要使用一个共同的对象实例锁定,例如你的类或参数的静态成员传递给两个线程。
You are creating a new object instance when the TryLock
method is called, and use that for locking. If you want mutual exclusion between the two threads, you need to use a common object instance for locking, e.g. a static member of your class or a parameter that you pass to both threads.
这篇关于asp.net多线程与的SyncLock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!