修改列表(结构)中的项目属性

发布于 2024-12-25 05:05:53 字数 609 浏览 0 评论 0原文

在(结构)列表中,我经常必须使用此代码修改项目的属性,

Private Sub ChangeState(ByVal ww As WebWorker, _
                        ByVal NewState As WorkerState)
        Dim oWBB As WebWorker = ListWebWorkers.Find(Function(item As WebWorker) item.Browser.Name.ToLower = ww.Browser.Name.ToLower)
        If oWBB.Browser IsNot Nothing Then
            ListWebWorkers.Remove(oWBB)
            oWBB = ww
            oWBB.State = NewState
            ListWebWorkers.Add(oWBB)
        End If
    End Sub

但是当两个或多个项目调用此子过程时,这会出现问题。其中一项可能已被删除。这段代码是在ui线程中执行的,并且必须如此

,那么有没有更好的方法来修改列表结构中的项目?

谢谢

In a list(of structure), i constantly have to modifiy the property of items using this code

Private Sub ChangeState(ByVal ww As WebWorker, _
                        ByVal NewState As WorkerState)
        Dim oWBB As WebWorker = ListWebWorkers.Find(Function(item As WebWorker) item.Browser.Name.ToLower = ww.Browser.Name.ToLower)
        If oWBB.Browser IsNot Nothing Then
            ListWebWorkers.Remove(oWBB)
            oWBB = ww
            oWBB.State = NewState
            ListWebWorkers.Add(oWBB)
        End If
    End Sub

But this give problem when two or more of the items call this sub procedure. in that one item may already have been removed. This code is executed in the ui thread, and has to be

so is there a better way to modify the items in list structure?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

墨小墨 2025-01-01 05:05:53

在您当前的代码中,您没有检查正确的项目是否存在(您正在检查oWBB.Browser,但您应该检查oWBB。此外,它不是线程-安全。

如果您使用 ConcurrentDictionary 相反,

这是重写代码的示例:

' Create a dictionary with case-insensitive keys
Private Shared ListWebWorkers As New System.Collections.Concurrent.ConcurrentDictionary(Of String, WebWorker)(StringComparer.InvariantCultureIgnoreCase)

Private Sub ChangeState(ByVal ww As WebWorker, ByVal NewState As WorkerState)

    If ListWebWorkers.ContainsKey(ww.Browser.Name) Then
        ListWebWorkers.TryRemove(ww.Browser.Name)
        ww.State = NewState
        ListWebWorkers.TryAdd(ww.Browser.Name, ww)
    End If
End Sub

In your current code, you are not checking the correct item for existence (you are checking oWBB.Browser, but you should be checking oWBB. In addition, it is not thread-safe.

It would be easier to verify the items existence in a threadsafe manner if you used a ConcurrentDictionary instead.

Here's an example of the rewritten code:

' Create a dictionary with case-insensitive keys
Private Shared ListWebWorkers As New System.Collections.Concurrent.ConcurrentDictionary(Of String, WebWorker)(StringComparer.InvariantCultureIgnoreCase)

Private Sub ChangeState(ByVal ww As WebWorker, ByVal NewState As WorkerState)

    If ListWebWorkers.ContainsKey(ww.Browser.Name) Then
        ListWebWorkers.TryRemove(ww.Browser.Name)
        ww.State = NewState
        ListWebWorkers.TryAdd(ww.Browser.Name, ww)
    End If
End Sub
¢好甜 2025-01-01 05:05:53

您必须锁定非线程安全的资源。这可确保在给定时间只有一个线程正在访问它们。

Private Sub ChangeState(ByVal ww As WebWorker, _ 
                    ByVal NewState As WorkerState) 

    SyncLock ListWebWorkers
        Dim oWBB As WebWorker = ListWebWorkers.Find(Function(item As WebWorker) item.Browser.Name.ToLower = ww.Browser.Name.ToLower) 
        If oWBB.Browser IsNot Nothing Then 
            ListWebWorkers.Remove(oWBB) 
            oWBB = ww 
            oWBB.State = NewState 
            ListWebWorkers.Add(oWBB) 
        End If 
    End SyncLock 
End Sub 

You have to lock the resources that are not thread safe. This ensures that only one thread is accessing them at a given time.

Private Sub ChangeState(ByVal ww As WebWorker, _ 
                    ByVal NewState As WorkerState) 

    SyncLock ListWebWorkers
        Dim oWBB As WebWorker = ListWebWorkers.Find(Function(item As WebWorker) item.Browser.Name.ToLower = ww.Browser.Name.ToLower) 
        If oWBB.Browser IsNot Nothing Then 
            ListWebWorkers.Remove(oWBB) 
            oWBB = ww 
            oWBB.State = NewState 
            ListWebWorkers.Add(oWBB) 
        End If 
    End SyncLock 
End Sub 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文