BibekWeb

Im working on desktop Application in C#.
When ever I close my application(My desktop Poject) following error pop on the IDE.
"Collection was modified; enumeration operation may not execute."
Some time it appears in Program.cs file and somtime in my main form designer class.

Please rply me if any one has solution regarding this problem.



Re: Visual C# General InvalidOperation Exception was unhandled

TaylorMichaelL

I assume then that the error is occuring within your application rather than the IDE itself. If you're not sure then try running your app outside the IDE to verify.

The error sounds like your form is trying to work with a collection after the form has closed. This is common when dealing with event handlers. The call stack should give you a better idea of what is going wrong. It is accessible from the exception object.

Michael Taylor - 6/12/07

http://p3net.mvps.org





Re: Visual C# General InvalidOperation Exception was unhandled

Peter Ritchie

Sounds like you're using foreach to iterate a collection and that collection is either modified in that loop or has been modified in another thread while the foreach loop was running. Do you have any such foreach loops in your application




Re: Visual C# General InvalidOperation Exception was unhandled

BibekWeb

hello Michael,

Thanks for Reply

Im not able to figure out this problem yet. Ya It is my application(my desktop application) problem not an IDE.

I have used thread for different purpose. I thought the problem is due to the improper disposing of thread but I have found anything later.

I tried to track the exception in call stack but when the exception occur the call stack get clear.
I have also created Error log system to track the error but this error escape . I dont have any information from which line of code this error is poping out .

Even at "application Close mehthod" I have dispose all the components and threads but Error is still there.

I m posting the detail error information.

Collection was modified; enumeration operation may not execute.
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at System.Collections.Generic.List`1.Enumerator.MoveNext()
at System.Data.SQLite.SQLiteConnection.Close()
at System.Data.SQLite.SQLiteConnection.Dispose(Boolean disposing)
at System.ComponentModel.Component.Finalize()

Thanks for your kind support and help.
Looking forward for ur suggestion and help.

best regards,
Bibek





Re: Visual C# General InvalidOperation Exception was unhandled

BibekWeb

hello Peter

Thanks for Reply.

Well there is nothing like that foreach iteration and collection problem.
As Im using thread and eventhandler in my project. there are many independent background processes which run for automatic processing. I suspect due to those Im facing those error.
But later tday I came to know it is not due to that also.. but Im still not sure.


Below is code of Close Method

private void btnClose_Click(object sender, EventArgs e)
{
//play sound.
Utility.PlaySound(eSoundCondition.CLICK);

if (Utility.Confirmation("½KÁË £¿", MessageBoxButtons.YesNo) == DialogResult.Yes)
{

//TODO : need to cancel opened orders for all the TWS.
//check for opened order which have been by current client or session.
if (GlobalResources.IsMultiAccountEnabled)
{
//
}
else
{
if (Utility.CheckForOrdersBeingProcessed(GlobalResources.Con))
{
//Take user confirmation if he/she wants to cancell opened orders.
string msg = string.Empty;
//msg = "Some of the orders are being porcesssed by TWS(IB)\n"
// + "Do you want to cancel the orders and close the application ";
msg = "Ž× ×¢ÎÄ TWS(IB) Àí ¡£\n"
+ "×¢ÎÄ ¡¢ ¡£";

if (Utility.Confirmation(msg, MessageBoxButtons.YesNo) == DialogResult.Yes)
{
Utility.CancelOpenedOrders(GlobalResources.Con, GlobalResources.tws, 0);
}
else
{
//don't close the application if he
return;
}
}
}

Utility.CloseTWS();
CloseApplication(); // AT this point I get error
}

}

This code can give u some idea abut my problem.

looking forward for your rply.

Best regards,
Bibek





Re: Visual C# General InvalidOperation Exception was unhandled

TaylorMichaelL

The call stack indicates the problem. The SQLite component is enumerating a list when it is being finalized. The list changes and the exception occurs. Did you write this component It should not be accessing reference type fields in its finalizer under any circumstances as the field could have already been released. As Peter mentioned (and the call stack confirms) the enumerator is blowing up because the collection has changed while it was being enumerated. The SQLite component will need to be fixed because it is working incorrectly.

Michael Taylor - 6/13/07

http://p3net.mvps.org





Re: Visual C# General InvalidOperation Exception was unhandled

Peter Ritchie

TaylorMichaelL wrote:

The call stack indicates the problem. The SQLite component is enumerating a list when it is being finalized. The list changes and the exception occurs. Did you write this component It should not be accessing reference type fields in its finalizer under any circumstances as the field could have already been released. As Peter mentioned (and the call stack confirms) the enumerator is blowing up because the collection has changed while it was being enumerated. The SQLite component will need to be fixed because it is working incorrectly.

Michael Taylor - 6/13/07

http://p3net.mvps.org

The latest code for SQL Lite's SqlLiteConnection.Dispose (http://sqlite-dotnet2.cvs.sourceforge.net/sqlite-dotnet2/SQLite.NET/System.Data.SQLite/SQLiteConnection.cs revision=1.67&view=markup) shows that Close is being called correctly:

Code Snippet
337 protected override void Dispose(bool disposing)
338 {
339 base.Dispose(disposing);
340
341 if (disposing)
342 Close();
343 }

Bibek, what version of SQLLite are you using If you still encounter problems, I suggest you log a bug at http://sourceforge.net/projects/sqlite-dotnet2






Re: Visual C# General InvalidOperation Exception was unhandled

TaylorMichaelL

Thanks for the code link Peter. That makes it easier to see what is going on. The component does in fact have a bug in the latest version. It might have already been reported. Here is what is happening.

Dispose is called in the finalizer.

Dispose calls Close.

Close copies the command list field (which is a reference type) to a local array for processing.

Close should not attempt to do anything with any reference types (weak reference or otherwise) if it was called from Dispose during finalization. The list itself is a reference type and might have already been freed. The only thing that is remotely confusing here is why it was able to start enumeration before dying (or maybe it didn't and it just generates the same message).

Michael Taylor - 6/13/07

http://p3net.mvps.org





Re: Visual C# General InvalidOperation Exception was unhandled

Peter Ritchie

TaylorMichaelL wrote:

Thanks for the code link Peter. That makes it easier to see what is going on. The component does in fact have a bug in the latest version. It might have already been reported. Here is what is happening.

Dispose is called in the finalizer.

Dispose calls Close.

Close copies the command list field (which is a reference type) to a local array for processing.

Close should not attempt to do anything with any reference types (weak reference or otherwise) if it was called from Dispose during finalization. The list itself is a reference type and might have already been freed. The only thing that is remotely confusing here is why it was able to start enumeration before dying (or maybe it didn't and it just generates the same message).

Michael Taylor - 6/13/07

http://p3net.mvps.org

The finalizer *should* be calling Dispose with false as the parameter. From that code snippet you can't tell if there is a bug in the current version. I can't tell if Bibek's call stack has true or false when Dispose(bool) is called from the finalizer (it *should* be false as it's finalizing, not disposing). It appears both Dispose(bool) and Close() are implemented correctly. I can't tell from where Dispose(bool) is being called.




Re: Visual C# General InvalidOperation Exception was unhandled

BibekWeb

hello Peter, hello Michael

I appreciate your help and suggestions. I m still facing the problem. Im using SQlite 3. Can i use code which peter had given on our previous dicussion.

I feel that the problem is causing my thread.
Is there is possiblity of thread problem


Please do suggest me.

With best regards,
Bibek







Re: Visual C# General InvalidOperation Exception was unhandled

TaylorMichaelL

The component implements IDisposable. Therefore you should wrap all calls to it in a using statement. This is: a) good programming practice, and b) solves the problem.

using(SqlLiteConnection conn = new ...)
{

...

};

Michael Taylor - 6/15/07

http://p3net.mvps.org