2 years ago

#16812

test-img

Joe_K

Application.Exit() throws System.InvalidOperationException: 'Collection was modified...'

(Visual Studio 2022-Community -- .Net 5 -- Windows 10)

I have been fighting an issue for days and have finally given up and have to ask the "smart people" for help.

I'm in Visual Studio (2022-Community) debugging my application. When I click on the "close button" I close Internet connection, database connections, then exit the app. In my log file I see "=== Application closed ===" in the log so it's making it to the end of the main TRY - CATCH block. It then steps out to a "DoEvents()" (which I added just because I couldn't find anything else wrong and though that maybe some resource was waiting).

It even executes the Application.Exit() and moves to the curly-bracket after it:

                applog.Info("=== Application closed ===");
        }
        catch (Exception ex)
        {
            string strError = $"Application Error{Environment.NewLine}{ex.ToString()}";
            applog.Error(strError);
            MessageBox.Show(strError, "Application Error", MessageBoxButtons.OK);
        }

        Application.DoEvents();
        Application.Exit();
        
    }     //-  <--- Error is happening HERE
}

}

...then throws:

System.InvalidOperationException
  HResult=0x80131509
  Message=Collection was modified; enumeration operation may not execute.
  Source=System.Private.CoreLib
  StackTrace:
   at System.ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion() in /_/src/libraries/System.Private.CoreLib/src/System/ThrowHelper.cs:line 361
   at System.Collections.Generic.List`1.Enumerator.MoveNextRare() in /_/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs:line 1109
   at Microsoft.Data.Sqlite.SqliteConnectionPool.ReclaimLeakedConnections() in /_/src/Microsoft.Data.Sqlite.Core/SqliteConnectionPool.cs:line 168
   at Microsoft.Data.Sqlite.SqliteConnectionPool.Clear() in /_/src/Microsoft.Data.Sqlite.Core/SqliteConnectionPool.cs:line 113
   at Microsoft.Data.Sqlite.SqliteConnectionFactory.ReleasePool(SqliteConnectionPool pool, Boolean clearing) in /_/src/Microsoft.Data.Sqlite.Core/SqliteConnectionFactory.cs:line 129
   at Microsoft.Data.Sqlite.SqliteConnectionPoolGroup.Clear() in /_/src/Microsoft.Data.Sqlite.Core/SqliteConnectionPoolGroup.cs:line 59
   at Microsoft.Data.Sqlite.SqliteConnectionFactory.ClearPools() in /_/src/Microsoft.Data.Sqlite.Core/SqliteConnectionFactory.cs:line 146
   at Microsoft.Data.Sqlite.SqliteConnectionFactory.<.ctor>b__7_1(Object _, EventArgs _) in /_/src/Microsoft.Data.Sqlite.Core/SqliteConnectionFactory.cs:line 31
   at System.AppContext.OnProcessExit() in /_/src/libraries/System.Private.CoreLib/src/System/AppContext.cs:line 77

I've closed out the database connections. I even "disposed" them to make sure they were gone.

I know that the "Collection was modified..." exception means that you can't change a collection you are iterating through, but at this point my code isn't iterating through anything.

I'd appreciate any help that can be provided.

shutdown

0 Answers

Your Answer

Accepted video resources