cominterop excel and c#

  • Thread starter Thread starter Ariel Gimenez
  • Start date Start date
A

Ariel Gimenez

Hi
in my app im working with excel using cominterop when fished my work with
excel i execute this code

oWB.Close(null,null,null);
oXL.Workbooks.Close();
oXL.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject (oXL);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oWB);
oSheet=null;
oWB=null;
oXL = null;
GC.Collect();


but Excel.exe still remains in memory untill i unload my exe, any ideas?

Thanks
 
Ariel,
but Excel.exe still remains in memory untill i unload my exe, any ideas?

There are still intermediate objects (such as the Workbooks
collection) that haven't been released, and they might keep Excel
running.



Mattias
 
But Mattias,

In his code he's closed the workbooks, shut down Excel, released the
associated COM objects, and set them to null. So in what sense are there
any intermediate objects that haven't been released??

And in any event -- what would the solution be?

--Bob
 
Thanks Mattias and Bob, i dont know whats happen, but affortunately, build
release mode and running the executable closes the excel completely, the
problem was when running in debug mode from the vs.net 2003 ide
 
Bob,
In his code he's closed the workbooks,

Right, but not the workbook collection object returned here

oXL.Workbooks.Close();
^^^^^^^^^

To release that object you'd have to do

Excel.Workbooks wbs = oXL.Workbooks;
wbs.Close();
Marshal.ReleaseComObject(wbs);

And in any event -- what would the solution be?

Be really careful to release everything object you touch (which can
make the code really ugly) or force a garbage collection after you're
done with all the objects. Neither way is really good.



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top