Connecting Tech Pros Worldwide Forums | Help | Site Map

What is the best way to make extended IEnumerator safe?

Sasha
Guest
 
Posts: n/a
#1: Nov 15 '05
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



Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#2: Nov 15 '05

re: What is the best way to make extended IEnumerator safe?


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:ORrz3M6gDHA.1008@TK2MSFTNGP12.phx.gbl...[color=blue]
> 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[/color]
and[color=blue]
> 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
>
>[/color]


Frank Oquendo
Guest
 
Posts: n/a
#3: Nov 15 '05

re: What is the best way to make extended IEnumerator safe?


Sasha wrote:[color=blue]
> 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...[/color]

Why?


Sasha
Guest
 
Posts: n/a
#4: Nov 15 '05

re: What is the best way to make extended IEnumerator safe?


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" <franko@acadx.com> wrote in message
news:OoZD337gDHA.1700@TK2MSFTNGP10.phx.gbl...[color=blue]
> Sasha wrote:[color=green]
> > 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...[/color]
>
> Why?
>
>[/color]


Jeffrey Tan[MSFT]
Guest
 
Posts: n/a
#5: Nov 15 '05

re: What is the best way to make extended IEnumerator safe?



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: <ORrz3M6gDHA.1008@TK2MSFTNGP12.phx.gbl>
<OoZD337gDHA.1700@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: <usPDeT8gDHA.524@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" <franko@acadx.com> wrote in message
| news:OoZD337gDHA.1700@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?
| >
| >
|
|
|

Fergus Cooney
Guest
 
Posts: n/a
#6: Nov 15 '05

re: What is the best way to make extended IEnumerator safe?


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


Frank Oquendo
Guest
 
Posts: n/a
#7: Nov 15 '05

re: What is the best way to make extended IEnumerator safe?


Sasha wrote:[color=blue]
> Otherwise enumerator will have a logical error.
>
> What if someone added some stuff, but we already passed that area. We
> will miss one item...[/color]

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.


Frank Oquendo
Guest
 
Posts: n/a
#8: Nov 15 '05

re: What is the best way to make extended IEnumerator safe?


Thus spake Jeffrey Tan[MSFT]:
[color=blue]
> But now, for thread safety, I think you should lock the entire
> collection while enumerating.[/color]

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


Eric Gunnerson [MS]
Guest
 
Posts: n/a
#9: Nov 15 '05

re: What is the best way to make extended IEnumerator safe?


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:ORrz3M6gDHA.1008@TK2MSFTNGP12.phx.gbl...[color=blue]
> 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[/color]
and[color=blue]
> 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
>
>[/color]


Jeffrey Tan[MSFT]
Guest
 
Posts: n/a
#10: Nov 15 '05

re: What is the best way to make extended IEnumerator safe?



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" <franko@acadx.com>
| References: <ORrz3M6gDHA.1008@TK2MSFTNGP12.phx.gbl>
<OoZD337gDHA.1700@TK2MSFTNGP10.phx.gbl>
<usPDeT8gDHA.524@tk2msftngp13.phx.gbl>
<CMmY1f9gDHA.1544@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: <ehat6cDhDHA.1008@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
|
|
|

Closed Thread