Can I restart a foreach loop if collection changes?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How can I reset the collections within a foreach to be read as a change from
within the foreach loop then restart the foreach after collections has been
changed?

foreach(string invoice in findListBox.listBox2.Items)
{
listBox2.Items count changed, restart this foreach
}

Thanks for any help.
Trint
 
You could use break to exit the foreach loop. Around the foreach loop you
could create a loop to restart the foreach loop. Be aware that this is a
serious thread to hang up your application. Maybe you should redesign
 
That doesn't really tell me anything...I know how to break from a loop. What
kind of loop do you put "around" the foreach loop?
Thanks,
Trint
 
Set a bool flag before the loop to false

Within the loop set the flag to true, when necessary, and exit the loop.

After the loop, use some mechanism to test whether you need to go back and
re-do the loop
One way is to use

do
{
// loop goes here
} while flag
 
bool contin = true;
while (contin)
{
contin = false; // very important
foreach(string invoice in findListBox.listBox2.Items)
{
if (listBox2.Items count changed)
{
contin = true;
break;
}
}
}

As Marinus said, this can be a very inefficient method. Us it only if
the count rarely changes. If the count is likely to change often, find a
different algorithm (one which doesn't involve foreach)


--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
Hi,


foreach does not provide that feature, you could do something like

bool flag=true;
while( flag )
{
try{
foreach(string invoice in findListBox.listBox2.Items)
{
listBox2.Items count changed, restart this foreach
}
flag=false;
}catch{}
}

That should work, but I haven't tried .

cheers,
 
Hi,

IIRC you will get an exceptioon, that's why you have to include a try/catch


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


James Curran said:
bool contin = true;
while (contin)
{
contin = false; // very important
foreach(string invoice in findListBox.listBox2.Items)
{
if (listBox2.Items count changed)
{
contin = true;
break;
}
}
}

As Marinus said, this can be a very inefficient method. Us it only if
the count rarely changes. If the count is likely to change often, find a
different algorithm (one which doesn't involve foreach)


--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

TrintCSD said:
That doesn't really tell me anything...I know how to break from a loop. What
kind of loop do you put "around" the foreach loop?
Thanks,
Trint
 
Ignacio Machin ( .NET/ C# MVP ) said:
IIRC you will get an exceptioon, that's why you have to include a
try/catch

In that case, I'd go for a slightly different approach.

bool contin = true;
while (contin)
{
contin = false; // very important
foreach(string invoice in findListBox.listBox2.Items)
{
if (listBox2.Items about to change)
{
contin = true;
break;
}
}
if (contin)
{
change listBox2.Items
}
}

Never use an exception as part of the normal processing.

But at this point, we have to ask --- exactly what is going on inside that
foreach()? (so we can design a proper algorithm for it.)

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

James Curran said:
bool contin = true;
while (contin)
{
contin = false; // very important
foreach(string invoice in findListBox.listBox2.Items)
{
if (listBox2.Items count changed)
{
contin = true;
break;
}
}
}

As Marinus said, this can be a very inefficient method. Us it only if
the count rarely changes. If the count is likely to change often, find a
different algorithm (one which doesn't involve foreach)


--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

TrintCSD said:
That doesn't really tell me anything...I know how to break from a loop. What
kind of loop do you put "around" the foreach loop?
Thanks,
Trint
--
Trinity Smith
c#/vb.Net Developer
EcoQuest, Intl.


:

You could use break to exit the foreach loop. Around the foreach loop you
could create a loop to restart the foreach loop. Be aware that this is
a
serious thread to hang up your application. Maybe you should redesign

:

How can I reset the collections within a foreach to be read as a change from
within the foreach loop then restart the foreach after collections
has been
changed?

foreach(string invoice in findListBox.listBox2.Items)
{
listBox2.Items count changed, restart this foreach
}

Thanks for any help.
Trint
 
How can I reset the collections within a foreach to be read as a
change from within the foreach loop then restart the foreach after
collections has been changed?

foreach(string invoice in findListBox.listBox2.Items)
{
listBox2.Items count changed, restart this foreach
}

IF you are wanting to restart the foreach because you are adding/removing
items, and you just to avoid an exception, then you really wouldn't want to
do this. Instead, create a new ArrayList from the listBox2.Items, and
iterate over that list. That way you don't run into the problem of the
collection changing during the foreach.

ArrayList a = new ArrayList(findlistBox.listBox2.Items);
foreach(string invoice in a)
{ ... }
 
Hi,

The problem is that probably the collection change in another thread, so it
may happen at anytime.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



James Curran said:
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote
in message news:[email protected]...
IIRC you will get an exceptioon, that's why you have to include a
try/catch

In that case, I'd go for a slightly different approach.

bool contin = true;
while (contin)
{
contin = false; // very important
foreach(string invoice in findListBox.listBox2.Items)
{
if (listBox2.Items about to change)
{
contin = true;
break;
}
}
if (contin)
{
change listBox2.Items
}
}

Never use an exception as part of the normal processing.

But at this point, we have to ask --- exactly what is going on inside that
foreach()? (so we can design a proper algorithm for it.)

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

James Curran said:
bool contin = true;
while (contin)
{
contin = false; // very important
foreach(string invoice in findListBox.listBox2.Items)
{
if (listBox2.Items count changed)
{
contin = true;
break;
}
}
}

As Marinus said, this can be a very inefficient method. Us it only if
the count rarely changes. If the count is likely to change often, find a
different algorithm (one which doesn't involve foreach)


--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

That doesn't really tell me anything...I know how to break from a
loop.
What
kind of loop do you put "around" the foreach loop?
Thanks,
Trint
--
Trinity Smith
c#/vb.Net Developer
EcoQuest, Intl.


:

You could use break to exit the foreach loop. Around the foreach
loop
you
could create a loop to restart the foreach loop. Be aware that this is
a
serious thread to hang up your application. Maybe you should
redesign

:

How can I reset the collections within a foreach to be read as a
change from
within the foreach loop then restart the foreach after collections
has
been
changed?

foreach(string invoice in findListBox.listBox2.Items)
{
listBox2.Items count changed, restart this foreach
}

Thanks for any help.
Trint
 
Hi,

If you want to make it thread safe, then create it like:

private ArrayList m_array = ArrayList.Synchronized(new ArrayList());

and do:

lock(m_array.SyncRoot)
{
foreach(object obj in m_array)
{
// do your stuff here
}
}

that way, if other thread will try to use your synchronized array while
you are in the foreach loop, it will wait for the loop to finish.

Eyal.
 
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us> said:
The problem is that probably the collection change in another thread, so it
may happen at anytime.

The collection *shouldn't* be changing in another thread - if it is,
that's a different problem. This is a UI collection we're talking
about, which means that:

a) the enumeration should be occurring in the UI thread
b) no other thread should be changing it
 
Hi,

Then I concord with James that the OP should post the content of the
foreach.

where else the collection can be changed if not inside the foreach?


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
 
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us> said:
Then I concord with James that the OP should post the content of the
foreach.

where else the collection can be changed if not inside the foreach?

Absolutely - it must be within the foreach, at least if the OP's
program is handling threading correctly.
 
Back
Top