English 中文(简体)
页: 1
原标题:asp.net multithreading with synclock

i) 有一些测试代码,每载一页,载于我的印本网网站。

这是法典。

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

“race”只是在 d的原木表中添加一个记录。

现在,正确的结果是,如果是“进入”、“退出”、“退出”的,就应当有 d条目。

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?

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

i 赞赏任何人的帮助

非常感谢!

http://www.un.org。

作为对萨沙奇的教职,一把我的法典改为一等(正处在一个单元),现在它喜欢:

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

now it works 80-90% of the time. on page load i have:

Dim cc = New CheckClass
    cc.TryThreads()

如果有一页多页,它们仍会发生冲突。 但如果一刀切,现在问题就好像在网上。 cache,因为当使用标准财产时,该法典在一页运行100%。

这样,我怎么能够确保两条read子,即使完全不同的会议也永远不会同时驾 the?

感谢大家帮助

问题回答

当要求使用方法并使用该方法锁定时,你正在创建新的物体。 如果你想要在两条路之间相互排斥,你需要利用一个共同的物体来锁定,例如,你的班级的固定成员或你穿过两面的参数。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签