<ch**********@h otmail.comwrote in message
news:11******** **************@ b58g2000hsg.goo glegroups.com.. .
On Apr 18, 3:48 pm, "Herfried K. Wagner [MVP]" <hirf-spam-me-
h...@gmx.atwrot e:
><chuckie_9...@ hotmail.comschr ieb:
I am struggling with releasing com objects. I
have isolated the problem to the code below. Objects are released and
the process ends until I
use "int k = sheet.Count;" Then the process does not end.
PRB: Office Application Does Not Quit After Automation from Visual Studio
.NET Client
<URL:http://support.microso ft.com/?scid=kb;EN-US;317109>
-"Troubleshootin g"
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
thank you for your resposnse and I have to admit I am somewhat
confused (I guess that's obvious). I read the article and maybe I
missed the obvious, but I do use GC.Collect() and
GC.WaitForPendi ngFinalizers() and excelapplicatio n.quit(). And I
think I understand that I must explicitly reference any object that is
implicitly created. So, I think (I am probably wrong) int k =
sheet.Count; creates an implicit reference that I must explicitly
reference so I can remove the reference. Is this correct? If so , how
do I do that? If I am wrong, please correct me. Everything works
fine until this statement is added. Thank you
No, Count does not create an reference to a COM object, sheet holds a reference to the COM
interface.
But as long as you don't post a *complete* sample, that illustrates the issue, you won't get
any sensible answers.
A complete sample looks something like this:
using System;
using System.Collecti ons.Generic;
using System.Text;
using System.Globaliz ation;
using System.Reflecti on;
using Exl = Microsoft.Offic e.Interop.Excel ;
namespace OffExc
{
class Program
{
[STAThread]
static void Main(string[] args)
{
System.Threadin g.Thread.Curren tThread.Current Culture = new CultureInfo( "en-US",
false );
Exl.Application exApp = new Microsoft.Offic e.Interop.Excel .ApplicationCla ss();
// Reference 1
Exl.Workbook wb = exApp.Workbooks .Add(Missing.Va lue); // Reference 2
exApp.Visible = true;
// Keep Excel visible for a while..
System.Threadin g.Thread.Sleep( 3000);
Exl.Sheets sheet = wb.Worksheets; // // Reference 3
int k = sheet.Count;
// Quit
exApp.Quit();
// Release the three COM references...
System.Runtime. InteropServices .Marshal.Releas eComObject(shee t);
System.Runtime. InteropServices .Marshal.Releas eComObject(wb);
System.Runtime. InteropServices .Marshal.Releas eComObject(exAp p);
GC.Collect();
// Let the finalizer run, this one will delete the unmanaged COM wrappers,
// if for one or another reason, the finalizer cannot run to completion,
// chances are that the Excel process won't get removed !!!!!!!!
GC.WaitForPendi ngFinalizers();
// Excel should be gone by now... keep the console processrunning for a while
System.Threadin g.Thread.Sleep( 30000);
}
}
}
And above code works as expected on my box.
Willy.