By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,056 Members | 1,296 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,056 IT Pros & Developers. It's quick & easy.

Lock generic SortedList

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
Ok! Great! Thanks a bunch!
Just wanted a second opinion since its 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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.