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

What is the best way to make extended IEnumerator safe?

P: n/a
Hi,

I am extending standard IEnumerator, and I was just wondering what is the
best way to make enumarator safe? What do I mean by safe? Detect deletes and
all...
My idea is to have private Guid state field in the collection, and every
time something is inserted or deleted from the collection, I will just
change the guid. Enumerator will just have to compare the guid received in
the begging to the current one. If they are different, the collection has
changed.

What are your thought on this?

Thank you,
Sasha
Nov 15 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Sasha,

That sounds like a good idea, and it would be quick and easy as well.
The only things you have to keep in mind is to lock the collections or
rather make sure that the guid is accessed correctly by multiple threads.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- nick(dot)paldino=at=exisconsulting<dot>com

"Sasha" <no@no.com> wrote in message
news:OR**************@TK2MSFTNGP12.phx.gbl...
Hi,

I am extending standard IEnumerator, and I was just wondering what is the
best way to make enumarator safe? What do I mean by safe? Detect deletes and all...
My idea is to have private Guid state field in the collection, and every
time something is inserted or deleted from the collection, I will just
change the guid. Enumerator will just have to compare the guid received in
the begging to the current one. If they are different, the collection has
changed.

What are your thought on this?

Thank you,
Sasha

Nov 15 '05 #2

P: n/a
Sasha wrote:
Hi,

I am extending standard IEnumerator, and I was just wondering what is
the best way to make enumarator safe? What do I mean by safe? Detect
deletes and all...


Why?
Nov 15 '05 #3

P: n/a
Otherwise enumerator will have a logical error.

What if someone added some stuff, but we already passed that area. We will
miss one item...

Take Care,
Sasha

"Frank Oquendo" <fr****@acadx.com> wrote in message
news:Oo**************@TK2MSFTNGP10.phx.gbl...
Sasha wrote:
Hi,

I am extending standard IEnumerator, and I was just wondering what is
the best way to make enumarator safe? What do I mean by safe? Detect
deletes and all...


Why?

Nov 15 '05 #4

P: n/a

Hi Sasha,

I think your idea is a good thought.

But now, for thread safety, I think you should lock the entire collection
while
enumerating.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Sasha" <no@no.com>
| References: <OR**************@TK2MSFTNGP12.phx.gbl>
<Oo**************@TK2MSFTNGP10.phx.gbl>
| Subject: Re: What is the best way to make extended IEnumerator safe?
| Date: Thu, 25 Sep 2003 17:31:46 -0800
| Lines: 22
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <us*************@tk2msftngp13.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: filenet-gw.filenet.com 198.3.8.1
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:187445
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Otherwise enumerator will have a logical error.
|
| What if someone added some stuff, but we already passed that area. We will
| miss one item...
|
| Take Care,
| Sasha
|
| "Frank Oquendo" <fr****@acadx.com> wrote in message
| news:Oo**************@TK2MSFTNGP10.phx.gbl...
| > Sasha wrote:
| > > Hi,
| > >
| > > I am extending standard IEnumerator, and I was just wondering what is
| > > the best way to make enumarator safe? What do I mean by safe? Detect
| > > deletes and all...
| >
| > Why?
| >
| >
|
|
|

Nov 15 '05 #5

P: n/a
Hi Sasha,

You've posted this same question separately in the languages.vb group. You
are not allowing the VB people to get the benefit of the CSharp people's
thinking, and vice versa. It fragments the discussion. It also means that
people like myself, who frequent both camps, have to decide which to post
comments into or to have to post to both.

It's not helped by the fact that this is occuring when the newsgroup
server and Outkooky Express are collaborating in keeping everyone confused as
to what exists and what has been deleted!!

Please, for future discussions (and they are most welcome), post to all
groups at the saame time.

Thanks. :-)

Regards,
Fergus
Nov 15 '05 #6

P: n/a
Sasha wrote:
Otherwise enumerator will have a logical error.

What if someone added some stuff, but we already passed that area. We
will miss one item...


That is clearly spelled out in the documentation on IEnumerator: changes
to the underlying collection invalidate the current enumerator and
causes an exception to be thrown.

What you're calling "safe" is completely counter to the definition an
enumerator.
Nov 15 '05 #7

P: n/a
Thus spake Jeffrey Tan[MSFT]:
But now, for thread safety, I think you should lock the entire
collection while enumerating.


If the collection is locked, only the current thread would be capable of
modifying it. So if it's your own code that's modifying the collection
during iteration, how are you going to miss it?

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
Nov 15 '05 #8

P: n/a
The simplest thing to do here is to keep an operation count as a member of
the collection, and increment it every time you modify the collection. The
enumerator stores the number when it starts an interation, and throws an
exception if the value changes.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Sasha" <no@no.com> wrote in message
news:OR**************@TK2MSFTNGP12.phx.gbl...
Hi,

I am extending standard IEnumerator, and I was just wondering what is the
best way to make enumarator safe? What do I mean by safe? Detect deletes and all...
My idea is to have private Guid state field in the collection, and every
time something is inserted or deleted from the collection, I will just
change the guid. Enumerator will just have to compare the guid received in
the begging to the current one. If they are different, the collection has
changed.

What are your thought on this?

Thank you,
Sasha

Nov 15 '05 #9

P: n/a

Hi Frank,

If there are multi-threading in your code to access it, you can design it
well,
making them visit it synchronization.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Frank Oquendo" <fr****@acadx.com>
| References: <OR**************@TK2MSFTNGP12.phx.gbl>
<Oo**************@TK2MSFTNGP10.phx.gbl>
<us*************@tk2msftngp13.phx.gbl>
<CM**************@cpmsftngxa06.phx.gbl>
| Subject: Re: What is the best way to make extended IEnumerator safe?
| Date: Fri, 26 Sep 2003 09:03:30 -0500
| Lines: 16
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <eh**************@TK2MSFTNGP12.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: unassigned-207-178-108-33.hubris.net 207.178.108.33
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:187546
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Thus spake Jeffrey Tan[MSFT]:
|
| > But now, for thread safety, I think you should lock the entire
| > collection while enumerating.
|
| If the collection is locked, only the current thread would be capable of
| modifying it. So if it's your own code that's modifying the collection
| during iteration, how are you going to miss it?
|
| --
| There are 10 kinds of people. Those who understand binary and those who
| don't.
|
| http://code.acadx.com
|
|
|

Nov 15 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.