Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / August 2006

Tip: Looking for answers? Try searching our database.

Excel process not getting terminated from memory.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sarang Bapat - 03 Aug 2006 09:58 GMT
Hi All,
I am automating excel using C#. I have created a .Net Class library which launches excel, and hooks to some of its events, and connects to an excel COM addin.
There are 2 scenarios in which excel is launched:
1. Excel is launched(through the above DLL), and an existing workbook is opened WITHOUT ANY USER INTERACTION. User performs some operations on the workbook, and closes excel. Excel is released by the launcher dll from the memory.
2. Excel is launched(through the above DLL), and excel File..Open fialog is presented to user. User selects the file to load in excel and loads it. User performs some operations on the workbook, and closes excel. Excel is released by the launcher dll from the memory.

Now, in scenario #1, the excel process is terminated properly from the memory and is automatically removed from the Task Manager. But in case of Scenarion #2, the excel process still remains in memory, though the same cleanup code runs to release excel. The only difference between the two operations is the introduction of the File...Open dialog code. The FileDialog object is released immidiately after its usage.

Here is how I open workbook in excel in Scenario #1:
              m_Workbook = m_ExcelApp.Workbooks.Open(docName, Missing.Value, false, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, true, Missing.Value, Missing.Value, true, Missing.Value, Missing.Value);

Here is how I open workbook in excel in Scenario #2:
               Microsoft.Office.Core.FileDialog fdlg = m_ExcelApp.Application.get_FileDialog(Microsoft.Office.Core.MsoFileDialogType.msoFileDialogOpen);
               
               fdlg.AllowMultiSelect = false;

               if (fdlg.Show() != 0)
               {
                   if (fdlg.SelectedItems.Item(1) != string.Empty)
                   {
                       
                       m_Workbook = m_ExcelApp.Workbooks.Open(fdlg.SelectedItems.Item(1), Missing.Value, false, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, true, Missing.Value, Missing.Value, true, Missing.Value, Missing.Value);
     }
 }
 //clean up file dialog
               System.Runtime.InteropServices.Marshal.ReleaseComObject(fdlg);
               fdlg = null;
               GC.Collect(); //Force Garbage collection after clean up. (See KBID - 317109)
               //end clean up

Here is the common cleanup code after user closes Excel:

               System.Runtime.InteropServices.Marshal.ReleaseComObject(m_Workbook); //release workbook
               System.Runtime.InteropServices.Marshal.ReleaseComObject(m_ExcelAddinApp); //release addin object
               System.Runtime.InteropServices.Marshal.ReleaseComObject(m_ExcelApp); //release excel application object

               m_Workbook = null;
               m_ExcelAddinApp = null;
               m_Workbook = null;

               //Force Garbage Collection after cleanup. (See KBID - 317109)
               GC.Collect();

Is there anything that I am missing out here?
Any quick help will be highly appreciated, since I am already on a tight schedule.

Thanks,
Sarang
Cindy M  -WordMVP- - 04 Aug 2006 16:39 GMT
Hi Sarang,

> But in case of Scenarion #2, the excel process still
> remains in memory, though the same cleanup code runs to
[quoted text clipped - 3 lines]
> after its usage.  Here is how I open workbook in excel
> in Scenario #1:

You mean Scenario #2, don't you?

According to Andrew Whitechapel's book on .NET Development
for Microsoft Office, you need to be extremely careful with
using ReleaseCOMObject, as this will effectively terminate
the RuntimeCallableWrapper (the reference to the COM
object). He recommends

   GC.Collect();
   GC.WaitFromPendingFinalizers();
   GC.Collect();
   GC.WaitFromPendingFinalizers();

You might try commenting out all calls to ReleaseCOMObject
and see if that makes any difference in what you see.

   -- Cindy
netrana@gmail.com - 17 Aug 2006 06:40 GMT
Hi Here is a quick solution. Hope it will work.

public class ResponseExport
{
  //First declare the API below the class declaration
  // API for excel automation
[DllImport("user32.dll", SetLastError = true)]
static extern void EndTask(IntPtr hWnd);

.. Other methods of the page.

public void WriteToExcel()
{
Excel.Application m_oExApp = new Excel.Application();

.. Do the excel related tasks here

// then write the following code
IntPtr iHandle = (System.IntPtr)m_oExApp.Hwnd;
EndTask(iHandle);
}
}

Hope now your problem is solved. If you still get the problem then
contact me.

> Hi All,
> I am automating excel using C#. I have created a .Net Class library which launches excel, and hooks to some of its events, and connects to an excel COM addin.
[quoted text clipped - 145 lines]
>
> ------=_NextPart_000_000A_01C6B709.23CE7740--

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.