VB.net ADO exception when exiting

brandonb

Diamond Member
Oct 17, 2006
3,731
2
0
I've been trying to add asyncronous queries to my application, however, I'm running into trouble doing so.

I've used the ADO .net wrapper to do the query, and I've dimmed it withevents.

Imports ADODB

Public Class DB

Private WithEvents ADO As ADODB.Connection

Public Sub New()

ADO = New ADODB.Connection
ADO.ConnectionTimeout = 30
ADO.CursorLocation = 3

End Sub

Protected Overrides Sub Finalize()

ADO.Cancel()

Select Case ADO.State
Case ConnectionState.Open, ConnectionState.Fetching, ConnectionState.Executing, ConnectionState.Connecting
ADO.Close()
End Select

ADO = Nothing

MyBase.Finalize()
End Sub

However, ADO = nothing crashes when exiting. About the RCW being lost or something to that effect. I've read around the web that .net can't handle WithEvents very well so you have to kill the com object directly.

Try
intl_COUNT = System.Runtime.InteropServices.Marshal.ReleaseComObject(ADO)
While intl_COUNT > 0
intl_COUNT = System.Runtime.InteropServices.Marshal.ReleaseComObject(ADO)
End While
Catch ex As Exception
Finally
GC.Collect()
GC.WaitForPendingFinalizers()
End Try

ADO = nothing

But ADO = Nothing still crashes with a different error (and the examples on the web indicate you can do that safely after doing the ReleaseComObject routine)

Does anybody know of a safe way to do this?

I don't want to be forced into using syncronous queries, which works very well btw.