i have a class that contains an arraylist. The arraylist is accessed from many threads. These threads add an item into arraylist.
In order to provide thread safety, i use lock keywork. But when i do searching on the arraylist by the for loop, i dont know if i should use lock or not.
// first usage
Expand|Select|Wrap|Line Numbers
- for(int i=0;i<myarraylist.Count;i++)
- {
- lock(myarraylist.SyncRoot)
- {
- // do whatevet on arraylist
- }
- }
Expand|Select|Wrap|Line Numbers
- lock(myarraylist.SyncRoot)
- {
- for(int i=0;i<myarraylist.Count;i++)
- {
- // do whatevet on arraylist
- }
- }