Enumeration already finished
I got an Error an enumeration already finish. How to check if an enumeration already finished?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Hemant SrivastavaRank #581 · 4.2K reputationPosted Aug 6, 2013, 12:29 PM EDT
David SmithRank #999 · 2K reputationPosted Aug 6, 2013, 1:05 PM EDT
en.MoveNext();
Just an example, if I keep trying to MoveNext, I will eventually get an Enumeration has already finished error.
VulpesRank #55 · 29.2K reputationPosted Aug 6, 2013, 1:33 PM EDT
However, if you're getting an InvalidOperationException, that suggests that the collection is being modified in some way while it's being enumerated which is not allowed.
David SmithRank #999 · 2K reputationPosted Aug 6, 2013, 4:04 PM EDT
Jignesh TrivediRank #16 · 50.3K reputationPosted Aug 6, 2013, 7:23 PM EDT
hi,
MoveNext() method return true or false
try
IEnumerator en = dataSetList.GetEnumerator();
while (en.MoveNext())
{
// do your code.
var myData = en.Current;
}
Please refer
http://msdn.microsoft.com/en-us/library/system.collections.ienumerator.movenext.aspx
hope this will help you.
VulpesRank #55 · 29.2K reputationPosted Aug 7, 2013, 12:57 AM EDT
Is it therefore possible that the collection is being modified on a different thread or by some process which you can't control directly?
David SmithRank #999 · 2K reputationPosted Aug 7, 2013, 10:26 AM EDT
Color prevColorOn;
Color prevColorOff;
IEnumerator en = dataSetList.GetEnumerator();
en.MoveNext();
prevOn = GetCycleInfo((string)en.Current);
if (prevColorOn.Type != ColorType.On)
{
throw new ArgumentException("prevColorOn.Type");
}
en.MoveNext();
prevColorOff = GetCycleInfo((string)en.Current); //Enumeration already finished
if (prevColorOff.Type != ColorType.Off)
{
throw new ArgumentException("prevColorOff.Type");
}
double ColorTimeUsed = (prevColor1.TimeTag - prevColor2.TimeTag).TotalSeconds;
for (int i = 0; i < (dataSetList.Count() / 2) - 1; ++i)
{
en.MoveNext();
Color newColorOn = GetColorInfo((string)en.Current);
en.MoveNext();
Color newColorOff = GetColorInfo((string)en.Current);
}
VulpesRank #55 · 29.2K reputationPosted Aug 7, 2013, 10:59 AM EDT
Usually, this property is left undefined and no exception is thrown but it looks like this particular enumerator has been coded to thrown an exception instead.
If the snippet is within a void method, then you could prevent the Current property being accessed when the enumeration has finished by replacing (wherever it occurs):
en.MoveNext();
with:
if (!en.MoveNext()) return;
However, you may want to take some further action before the method returns.
David SmithRank #999 · 2K reputationPosted Aug 7, 2013, 2:24 PM EDT
if (prevColorOn.Type != ColorType.ColorOn)
{
throw new ArgumentException("prevColorOn", "Did not recieved expected ColorType'prevColorOn'.");
}
VulpesRank #55 · 29.2K reputationPosted Aug 8, 2013, 1:23 AM EDT
David SmithRank #999 · 2K reputationPosted Aug 8, 2013, 11:09 AM EDT
private static Color GetColorInfo(string colorInfo)
{
try
{
const string FORMAT_SEQUENCE = "(^.+) (On|Off)";
Regex r = new Regex(FORMAT_SEQUENCE);
Match match = r.Match(colorInfo);
Color color = new Color();
color.ColorDateTime = dt;
color.ColorType = (match.Groups[2].ToString() == "On") ? ColorTimeType.On : ColorTimeType.Off;
return cycle;
}
catch( Exception)
{
throw;
}
}
---------------------------------------------------
This enumeration comparison below works, is there another way to do it without using the integer value of the enumeration type.
prevColorOn = GetColorInfo((string)en.Current);
if ((int)prevColorOn.Type != (int)ColorTimeType.On)
{
throw new ArgumentException("prevColorOn.Type", "Did not recieved expected ColorTimeType 'prevColorOn'.");
}
VulpesRank #55 · 29.2K reputationPosted Aug 8, 2013, 1:02 PM EDT
For example, if the string contained 'on' or 'ON' rather than 'On'.
If there was more than one occurrence (or no occurrences) of 'On' and 'Off' in the same string.
If 'On' or 'Off' was not preceded by a space etc.
As long as you're comparing two enum values of the same type (which I can now see is the case), then there's no need to cast to int first. So this should work OK as both operands are of type ColorTimeType:
if (prevColorOn.Type != ColorTimeType.On)
{
throw new ArgumentException("prevColorOn.Type", "Did not recieved expected ColorTimeType 'prevColorOn'.");
}