Background worker thread population of cells

77 views
Skip to first unread message

Andrew Knight

unread,
Sep 7, 2018, 6:25:32 PM9/7/18
to Excel-DNA
Hi all

Is using a background worker thread to populate cells off the main thread in an ExcelDNA add on recommended?

I'm only asking as i'm pulling my hair out trying to release the com objects on quit (excel stays open blank in debug and has to be closed manually a second time)...

I've de-registered all the handlers in use on workbook close from the main thread.

So im wondering if its the background worker thread that has still clinged onto references that arent releasing?

Andy

Mike Junkin

unread,
Sep 7, 2018, 9:54:56 PM9/7/18
to exce...@googlegroups.com
Excel COM objects are STA so any access to them have to be done from an STA thread with a message pump that pumps the proper COM messages. 

I’ve had success using a thread pool based off the standard .NET dispatcher.

--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to exceldna+u...@googlegroups.com.
To post to this group, send email to exce...@googlegroups.com.
Visit this group at https://groups.google.com/group/exceldna.
For more options, visit https://groups.google.com/d/optout.

Govert van Drimmelen

unread,
Sep 8, 2018, 2:39:59 AM9/8/18
to exce...@googlegroups.com
Hi Andrew,

I think an Excel-DNA add-in should _never_ access the Excel COM object model from another thread.
You should also make sure you are using the Application object that you get back from ExcelDnaUtil.Application as your root object.

If you want to schedule some code to run in a context where you can interact with Excel, you can use the ExcelAsyncUtil.QueueAsMacro(...) helper to make the transition to a safe context on the main thread.

-Govert



From: exce...@googlegroups.com [exce...@googlegroups.com] on behalf of Mike Junkin [mikej...@gmail.com]
Sent: 08 September 2018 03:54 AM
To: exce...@googlegroups.com
Subject: Re: [ExcelDna] Background worker thread population of cells

Mike Junkin

unread,
Sep 8, 2018, 9:04:12 PM9/8/18
to exce...@googlegroups.com
Why do you think Excel COM objects should never be used from any thread but Excels main window thread?  The COM STA rules are pretty well defined no?
Reply all
Reply to author
Forward
0 new messages