Problem with Thread and COM object

I'm working with an COM object that control some peripherals. I'm trying to start a new thread that will continuously doing something on that COM.

My first question is how should I do this I've tried the BackgroundWorker control. It seems quite easy to work at first. However, I've got an exception saying "COM object that has been separated from its underlying RCW cannot be used" when I close the application. After some research, I found that it maybe caused by the COM object that has been disposed while my thread is sleeping. What's the proper way to close the application By the way, do I need to close the thread by some means

I'm new to thread programming. Any suggestion is welcome.




Answer this question

Problem with Thread and COM object

  • Kari

    The RCW only keeps one reference to a COM object regardless of the number of managed clients calling it. If your GUI thread is releasing the COM object before your background thread is done with it (e.g. via a call to Marshal.ReleaseComObject) then the object will be gone when your background thread tries to use it. If you *must* create that particular COM object in the GUI thread then give it to the background thread, then you should at least call Marshal.AddRef before giving it to the background thread and the background thread should call Mashal.Relese when it's done with it. Otherwise, you should create a synchronization object to synchronize access to the object and use appropriate logic to manage and use that synchronization object and compensate for deadlocks/livelocks/starvation/etc.

  • PaulCowan

  • Jason1

    Can you point me to any code example

  • Problem with Thread and COM object