LinkedList(T) 需要一个 Syncroot
我正在使用 VB.Net 并且想使用 LinkedList。唯一的问题是它是一个多线程应用程序。我从MSDN看到Syncroot是ICollection接口的显式实现。我发现人们想用 List(Of T) 做类似的事情。看来解决方案是将列表投射到接口上。
我尝试在 VB.Net 中做类似的事情,基本上是:
Dim TestLinkedList = New LinkedList(Of Long)
SyncLock (Ctype(TestLinkedList, ICollection)).SyncRoot
.
.
.
End SyncLock
上面的内容正确吗?
I am using VB.Net and would like to use a LinkedList. Only problem is that it is a multithreaded application. I saw from MSDN that Syncroot is an explicit implementation of the ICollection Interface. I found people wanting to do similar things with List(Of T). It seems that the solution there is to cast the list to the interface.
I have tried to do what I would imagine to be a similar thing in VB.Net, basically:
Dim TestLinkedList = New LinkedList(Of Long)
SyncLock (Ctype(TestLinkedList, ICollection)).SyncRoot
.
.
.
End SyncLock
Is the above correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它会起作用,这就是它所能说的一切。 SyncRoot 是 .NET 1.1 中的一个错误,没有理由继续这种做法。
It will work, that's about all that can be said for it. SyncRoot was a mistake from .NET 1.1, there's no reason to continue the practice.
ICollection.SyncRoot
通常被认为是一个坏主意。您应该锁定集合本身,或者锁定您创建的单独的锁定对象ICollection.SyncRoot
is generally considered a bad idea. You should either lock the collection itself, or lock on a separate lock object you create