Hi Vickie,
Marshal.ReleaseComObject() forces the .NET COM object wrapper to
release the referenced COM object (in this case Excel's Application
object). If there are outstanding references to the Excel objects, the
Excel process will not terminate when you close Excel. You can test
this by removing the ReleaseComObject() call, calling the resizer
function, closing Excel and then checking in the Task Manager - Excel
will be 'closed' but the process will stick around.
So in the ArrayResizer example where the Application object is
accessed from a different thread, we either need to release the object
explicitly, or force a garbage collection. If we don't, and we close
Excel, the garbage collector will never run and clean up the COM
reference, since no further .NET code is running which could trigger
the GC.
I don't have a good idea of when this is required and when you don't
need it, but in this case we need it and it certainly is safe and
correct - we are done with the COM wrapper after the "Run" call
completes.
Multi-threaded UDF calculation is a totally different thing to making
your own threads behind Excel's back, and then trying to access Excel
from those. UDF calculations returning values to Excel are handled by
Excel internally. The multi-threaded recalculation runs on threads
that Excel controls and only some of the C API calls are supported
from the context of the thread-safe functions. I doubt accessing the
COM object model from this function context is safe.
The COM errors we are dealing with here only arise when Excel is
called through the COM automation interface.
Regards,
Govert
> > Two useful threads are:
http://social.msdn.microsoft.com/Forums/en/vsto/thread/1dab0874-bba5-......