BackgroundWorker thread - accessing ExcelApp after shutdown

Add-in Express™ Support Service
That's what is more important than anything else

BackgroundWorker thread - accessing ExcelApp after shutdown
 
nir zamir


Guest


Hi,

I have a BackgroundWorker object that does some manipulations on Excel objects.

IN DEBUG MODE, After shutting down Excel, if the thread tries to access ExcelApp, I get the following:

RaceOnRCWCleanup was detected
Message: An attempt has been made to free an RCW that is in use. The RCW is in use on the active thread or another thread. Attempting to free an in-use RCW can cause corruption or data loss.

When I press "Continue" the program exits (like it should).

Should it concern me? I couldn't avoid this by checking if ExcelApp is null.

Thanks,
Nir
Posted 25 Mar, 2007 12:18:59 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi Nir.

Do you stop the thread in the AddinBeginShutdown event handler?
The code should also wait until the thread stops.


P.S. Note that we take up your forum requests in the order we receive them.
Besides, it may take us some time to investigate your issue. Please be sure we will let you know as soon as the best possible solution is found.
Posted 26 Mar, 2007 16:20:13 Top
nir zamir


Guest


Hi Sergey,

Very frequently it happens that the thread never stops, since it seems to be "locked" on getting ExcelApp (in my thread I address ExcelApp to go over open workbooks). I only guess that it's because the Excel application is currently shutting down, but I don't understand why it never returns.
BTW, from what I've seen, ExcelApp is "locked" even before the "BeginShutdown" event is called.

Since BackgroundWorker can't really be aborted, I tried using Thread and Thread.Abort and still have this issue.
If I don't wait for the thread to be killed, the Excel process stays in the air after shutdown...


Can you please help?

Thanks,
Nir.
Posted 10 Apr, 2007 08:55:51 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi Nir.

Please download the following example:
http://www.add-in-express.com/projects/synchronizationtemplateexample.zip

The example is written for Outlook but you can use the same technique in Excel as well.
Posted 10 Apr, 2007 09:58:00 Top
nir zamir


Guest


Hi Sergey,

From my first trials - the whole secret was using Thread.Join()! So far so good - Excel seems to shut down correctly! Thanks a lot.

Reading the example, I have a few small questions regarding COM objects release. I just want to make sure I'm doing things properly:

2. In ThreadProc, isn't module.contacts.Items a COM object that has to be released?
3. Also in ThreadProc, who releases COM objects when ThreadAbortException is thrown?
4. In AddinModule_AddinStartupComplete, shouldn't the reference to Outlook.OlDefaultFolders be released when the function returns? Isn't an RCW created for it when referencing Outlook.OlDefaultFolders.olFolderContacts?

BTW, is there a place to find all such examples? they're very helpful.

Thanks a lot!
Nir
Posted 10 Apr, 2007 11:51:14 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
1. Yes, correct. In Outlook earlier 2003 this code may produce problems.
Please change it.
2. All objects should be released in the 'finally' blocks.
3. No. The 'contacts' object is released in the AddinBeginShutdown event handler because it is used in the ThreadProc().

BTW, is there a place to find all such examples? they're very helpful.

We will update our HOWTOs section soon.
Posted 10 Apr, 2007 12:04:30 Top