views:

145

answers:

2

I'm using Office Automation in .NET. It is leaving behind the excel.exe program. I know the fix - it is all about explicitely defining the variables. Once defined, I can properly de-allocate the ram and the GC will clean them up.

The problem is, I have literally thousands of lines of code to go through. So I'm wondering: Is there some sort of a utility in .net (or 3rd party) that is capable of showing me a list of variables for which I have ram allocated still? If so, I would be able to target those items and specificially de-allocate them.

Thanks

Ryan

A: 

Just to be clear, you need to explicitly Quit Excel after you are done using it through automation. If you don't an instance will keep running, even if your variables go out of scope and garbage collection runs. I believe the command it Quit():

Excel.ApplicationClass excel = new Excel.ApplicationClass();
//do some work with Excel
excel.Quit();

I would recommend you do this with a try/catch/finally

Excel.ApplicationClass excel = null;

try
{
  excel = new Excel.ApplicationClass();
  //do some Excel work
}
catch(Exception ex)
{
  //log exception
  throw;
}
finally
{
  if(excel != null)
    excel.Quit();
}
Jason Jackson