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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Aug 8, 2013, 5:02 PM
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'.");
}
David SmithPosted Aug 8, 2013, 3:09 PM
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'.");
}
VulpesPosted Aug 8, 2013, 5:23 AM
David SmithPosted Aug 7, 2013, 6:24 PM
if (prevColorOn.Type != ColorType.ColorOn)
{
throw new ArgumentException("prevColorOn", "Did not recieved expected ColorType'prevColorOn'.");
}
VulpesPosted Aug 7, 2013, 2:59 PM
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 SmithPosted Aug 7, 2013, 2:26 PM
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);
}
VulpesPosted Aug 7, 2013, 4:57 AM
Is it therefore possible that the collection is being modified on a different thread or by some process which you can't control directly?
Jignesh TrivediPosted Aug 6, 2013, 11:23 PM
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.
David SmithPosted Aug 6, 2013, 8:04 PM
VulpesPosted Aug 6, 2013, 5:33 PM
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 SmithPosted Aug 6, 2013, 5:05 PM
en.MoveNext();
Just an example, if I keep trying to MoveNext, I will eventually get an Enumeration has already finished error.
Hemant SrivastavaPosted Aug 6, 2013, 4:29 PM