--- lang: ja-jp breaks: true --- # C# List をスレッドセーフに取り扱うには、`System.Collections.ICollection`インターフェイスの`SyncRoot`を使用する 2021-07-28 ```csharp= List<string> lstDelete = new List<string>(); Parallel.ForEach(this.m_dicPhysicalAddress.Keys, key => { if (hasPAddress.Contains(key) == false) { lock (((System.Collections.ICollection)lstDelete).SyncRoot) { lstDelete.Add(key); } } }); Parallel.ForEach(lstDelete, key => { ClientPhysicalAddressDataSet.ClientRow rowDs = null; this.m_dicPhysicalAddress.TryRemove(key, out rowDs); }); ``` :::warning この方法は、Dictionary では使用できない。利用するとエラーとなる。 ::: ###### tags: `C#` `List<>` `スレッドセーフ` `System.Collections.ICollection` `SyncRoot`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up