473,395 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Lock generic SortedList

Hi,

Any comments regarding this implementation...

SortedList<String, Stringlist = new SortedList<String, String>();

lock (((IList)list).SyncRoot)
{
foreach (Object item in list)
{
//Do some stuff...
}
}

Regards
/Oscar
Oct 3 '06 #1
4 2703
Well, unless there are some missing lines that make the list available to
another thread, you don't really need to lock it anyway... of course, by
that reasoning the list is also empty, which I'm sure it isn't ;-p

IIRC, the base foreach of a dictionary is a KeyValuePair<TKey,TValue>; you
might choose to enumerate .Values or .Keys, which will allow direct access
to the strings; otherwise, using the base foreach I would cast "item"
correctly - i.e. as a KeyValuePair<string,string>.

If the list is reasonably short, and you want to minimise lock times, and
the specific scenario permits, then you could perhaps take a spanshot of the
values as an array first (within the lock), and then (outside the lock)
enumerate the items. This assumes you aren't reassigning / adding /
removing, which you can't do inside a foreach anyway (but you can do inside
the same lock, but it isn't shown).

Marc
Oct 3 '06 #2
seems OK.. except it may keep the list locked for some extra time depending
the operation you do there..

Nirosh.

"Oscar Thornell" <no****@internet.comwrote in message
news:OQ**************@TK2MSFTNGP03.phx.gbl...
Hi,

Any comments regarding this implementation...

SortedList<String, Stringlist = new SortedList<String, String>();

lock (((IList)list).SyncRoot)
{
foreach (Object item in list)
{
//Do some stuff...
}
}

Regards
/Oscar

Oct 3 '06 #3
Ok! Great! Thanks a bunch!
Just wanted a second opinion since it´s the first time I needed to deal with
thread saftey and generic collections..

/Oscar

"Champika Nirosh" <te**@tc.comwrote in message
news:u0**************@TK2MSFTNGP04.phx.gbl...
seems OK.. except it may keep the list locked for some extra time
depending the operation you do there..

Nirosh.

"Oscar Thornell" <no****@internet.comwrote in message
news:OQ**************@TK2MSFTNGP03.phx.gbl...
>Hi,

Any comments regarding this implementation...

SortedList<String, Stringlist = new SortedList<String, String>();

lock (((IList)list).SyncRoot)
{
foreach (Object item in list)
{
//Do some stuff...
}
}

Regards
/Oscar


Oct 3 '06 #4
If this is all you're doing, you don't need the lock (but you already knew
that).

That said, if you need to lock the collection this is a valid way to do it.
You can also look into Reader-Writer Locks depending on your use case.

You could also wrap your sorted list in the generic ReadOnlyCollection class
(this also implements IList), that way you could avoide the locks
altogether.

--
Chris Mullns, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins
"Oscar Thornell" <no****@internet.comwrote in message
news:OQ**************@TK2MSFTNGP03.phx.gbl...
Hi,

Any comments regarding this implementation...

SortedList<String, Stringlist = new SortedList<String, String>();

lock (((IList)list).SyncRoot)
{
foreach (Object item in list)
{
//Do some stuff...
}
}

Regards
/Oscar

Oct 3 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Rich Sienkiewicz | last post by:
Some classes, like Queue and SortedList, have a Synchronized method which gives a thread safe wrapper object for these classes. But the lock() statement does the same thing. Is there any rules as to...
2
by: orekinbck | last post by:
Hi There I am probably missing something fundamental here, but I cannot see a method to search the values of a generic dictionary so that I can find the key ? Of course I could enumerate...
2
by: newscorrespondent | last post by:
I have a list declared: public System.Collections.Generic.SortedList<int, System.Collections.Generic.List<MTGTracer ActiveList = new SortedList<int,List<MTGTracer>>(); The key is an integer...
2
by: Bruce Arnold | last post by:
I have a program that works fine using Remove and Add to update a value. The program processes a log file from a router and counts the hits based on url. It bothers me to use Remove/Add when all...
4
by: semedao | last post by:
Hi, I want to implement list of key-values that can be sort by 2 ways. let's say that in the first step I wanted to make SortList based on Key = int index that cannot change and Value is another...
6
by: Nick Valeontis | last post by:
I know how to use Icomparable. However, I can't figure out how to sort a generic linked list? (without writing the algorithm) Lets say I have something like this: class...
1
by: raylopez99 | last post by:
I seem to get name collision between the Generic collection SortedList and C++.NET Framework collection SortedList. How to resolve? Here are the libraries that seem to clash:...
2
by: active | last post by:
This is where a different thread ended. The subject of this query is quite different from that of the other thread so I thought I should start a new thread. I'm using Private mItemList As...
1
by: Ethan Strauss | last post by:
Hi, I have been using a variety of generic collections and I really like them, but I have just noticed something weird ... If you have a generic Dictionary (or SortedList), you can retrieve...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.