Excel always running

  • Thread starter Thread starter KaNos
  • Start date Start date
K

KaNos

Hello,
I'm programming an C# application which uses Automation. The problem is
when I quit the application a ghost of excel is always running (Can be
seen in all processes with th task manager). What is the solution ?
Thanks
 
KaNos,

You should make sure that even quitting the application, you set the object
to null. That would make the garbage collector do its job.

Besides that, you may have some sort of dialog opening to prevent you to
close the window. By then, use the Process.GetProcessesByName method and
kill each one of the instances.

Best,
 
You should make sure that even quitting the application, you set the object
to null. That would make the garbage collector do its job.
Not really, the GC is not aware of the unmanaged resources wrapped by the
managed resource. You need to place a strategic call to

TheExcelInstance.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel)

'this isn't strictly necessary
TheExcelInstance = Nothing

You should also note here that this may not release all instances if you
used improper programming techniquest to create the Excel object. For
instance, there may be temporary instances lying around.
 
I am having a similiar problem where a DLL I've created calls the Interop
assembly for Excel to some COM automation.
Since the DLL I've created is loaded into a host process I have no control
over when the host process is shutdown.
Here is some sample code which illustrates what I've seen create the
orphaned Excel process.
Any help on what I'm doing wrong here would be greatly appreciated.

Sincerely
John Fors
Imports Microsoft.Office.Interop.Excel

Module ExcelConsole

Sub Main()

Dim oApp As Microsoft.Office.Interop.Excel.Application

Dim oBook As Microsoft.Office.Interop.Excel.Workbook ' = oApp.Workbooks.Add

Dim oSheet As Microsoft.Office.Interop.Excel.Worksheet '= oApp.ActiveSheet

oApp = New Microsoft.Office.Interop.Excel.Application

oApp.Workbooks.Open("C:\test\tt.xls")

oBook = oApp.ActiveWorkbook

oSheet = oApp.ActiveSheet

Debug.WriteLine(oSheet.Name)

' Debug.Print(oApp.Run("Test",25))

oApp.Visible = True

System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet)

Debug.Print(System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oSheet))

oSheet = Nothing



oBook.Close(False)

oApp.Workbooks.Close()

System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook)

Debug.Print(System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oBook))

oBook = Nothing

oApp.Quit()

System.Runtime.InteropServices.Marshal.ReleaseComObject(oApp)

Debug.Print(System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oApp))

Try

oApp.Workbooks.Add()



Catch ex As Exception

Debug.WriteLine("Exception message")

End Try

oApp = Nothing

Debug.WriteLine("End Excel")

GC.Collect()

End Sub
 
Back
Top