InvalidComObjectException, threaded db access

  • Thread starter Thread starter 2G
  • Start date Start date
2

2G

Hi

From the moment I have threaded a function that loads data from the db , I
get the following error:

An unhandled exception of type
'System.Runtime.InteropServices.InvalidComObjectException' occurred in
system.data.dll
Additional information: COM object that has been separated from its
underlying RCW can not be used.

I searched a bit on this error and saw that it could have something to do
with Marshal.ReleaseComObject but I'm not using this.

If I wait until the thread has done running and restart the thread again,
all works fine, but this thread has to be restart the moment I click a
button.
Currently I 'm stopping the running thread with .Abort() and catching the
threadabortexception , I think this has something to do with the error I'm
getting as I suspect .Abort() is not the good way to go.

Does anyone know how I could solve this?

Thanks.
 
2G,

Abort is never a good thing. It calls TerminateThread, which I
understand can be pretty violent. I imagine that it doesn't do wonders for
COM apartments either. If possible, you should send a message to the thread
to indicate that it should stop processing.

Also, are you accessing the same instance of the database class from
multiple threads? If so, you should provide locks around the access, as I
believe most of the instance methods on classes in the System.Data namespace
are not thread safe.

Hope this helps.
 
Back
Top