How to Kill Excel Task

J

Joseph

Hello,
I am developing a program that will read data from a
couple of text files, process it, and then, populate data
into specific cells in an Excel spreadsheet.

Towards the end of the program, I try to exit from Excel
using the followings statements.

m_objBook.Close(false, m_objOpt, m_objOpt);
m_objExcel.Quit();

(Definitions are as follows...
Excel._Workbook m_objBook
object m_objOpt = System.Reflection.Missing.Value;
Excel.Application m_objExcel
)

The program ends successfully, but after I execute it a
couple of times, Excel stops responding when I try to
open the resultant file. When I go to task manager, I can
see several Excel.exe tasks out there (even if I don't
have any Excel file open) and I have to close off those
tasks before I can open any excel file.

Any suggestions on how to resolve this issue?
Any help would be greatly appreciated.

thanks!
 
N

Nicholas Paldino [.NET/C# MVP]

Joseph,

You will have to post more of your code. More likely than not, you are
not releasing every reference to an object in Excel. Basically whenever you
access a property, you are increasing the reference count of the main
application (since almost every object in Excel has a reference to
Application). Because of this, every time you get a return value from a
property or method which is an Excel object, you have to make sure you
decrement the reference count (usually a call to the static ReleaseComObject
method on the Marshal class).

If you have so much code that you can't check for this everywhere, then
you will want to make sure that all the managed references are null. Once
they are and you call Quit, you should be able to call the static Collect
method on the GC class and then the Excel instance will go away.

Hope this helps.
 
J

Joseph

Nicholas,
thank you for your suggestion. You are right about me
not releasing every (any) reference to the Excel objects.
I am fairly new to this platform and so, didn't know that
I had to release the references. I tried coding the
GC.collect(0) before the quit and the problem seems to
have been resolved.
One thing that I am not clear about is why this problem
was occurring at random (earlier)... It was not a
consistant problem - but I guess that must be because GC
must have been invoked by the system sometimes...
Is that correct?

thanks again!
-----Original Message-----
Joseph,

You will have to post more of your code. More likely than not, you are
not releasing every reference to an object in Excel. Basically whenever you
access a property, you are increasing the reference count of the main
application (since almost every object in Excel has a reference to
Application). Because of this, every time you get a return value from a
property or method which is an Excel object, you have to make sure you
decrement the reference count (usually a call to the static ReleaseComObject
method on the Marshal class).

If you have so much code that you can't check for this everywhere, then
you will want to make sure that all the managed references are null. Once
they are and you call Quit, you should be able to call the static Collect
method on the GC class and then the Excel instance will go away.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Joseph said:
Hello,
I am developing a program that will read data from a
couple of text files, process it, and then, populate data
into specific cells in an Excel spreadsheet.

Towards the end of the program, I try to exit from Excel
using the followings statements.

m_objBook.Close(false, m_objOpt, m_objOpt);
m_objExcel.Quit();

(Definitions are as follows...
Excel._Workbook m_objBook
object m_objOpt = System.Reflection.Missing.Value;
Excel.Application m_objExcel
)

The program ends successfully, but after I execute it a
couple of times, Excel stops responding when I try to
open the resultant file. When I go to task manager, I can
see several Excel.exe tasks out there (even if I don't
have any Excel file open) and I have to close off those
tasks before I can open any excel file.

Any suggestions on how to resolve this issue?
Any help would be greatly appreciated.

thanks!


.
 
N

Nicholas Paldino [.NET/C# MVP]

Joseph,

It is possible that the GC was being called by the system, but also,
because I don't know how you were accessing the Excel object model, I don't
know what you were cleaning up (or not) or how often.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Joseph said:
Nicholas,
thank you for your suggestion. You are right about me
not releasing every (any) reference to the Excel objects.
I am fairly new to this platform and so, didn't know that
I had to release the references. I tried coding the
GC.collect(0) before the quit and the problem seems to
have been resolved.
One thing that I am not clear about is why this problem
was occurring at random (earlier)... It was not a
consistant problem - but I guess that must be because GC
must have been invoked by the system sometimes...
Is that correct?

thanks again!
-----Original Message-----
Joseph,

You will have to post more of your code. More likely than not, you are
not releasing every reference to an object in Excel. Basically whenever you
access a property, you are increasing the reference count of the main
application (since almost every object in Excel has a reference to
Application). Because of this, every time you get a return value from a
property or method which is an Excel object, you have to make sure you
decrement the reference count (usually a call to the static ReleaseComObject
method on the Marshal class).

If you have so much code that you can't check for this everywhere, then
you will want to make sure that all the managed references are null. Once
they are and you call Quit, you should be able to call the static Collect
method on the GC class and then the Excel instance will go away.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Joseph said:
Hello,
I am developing a program that will read data from a
couple of text files, process it, and then, populate data
into specific cells in an Excel spreadsheet.

Towards the end of the program, I try to exit from Excel
using the followings statements.

m_objBook.Close(false, m_objOpt, m_objOpt);
m_objExcel.Quit();

(Definitions are as follows...
Excel._Workbook m_objBook
object m_objOpt = System.Reflection.Missing.Value;
Excel.Application m_objExcel
)

The program ends successfully, but after I execute it a
couple of times, Excel stops responding when I try to
open the resultant file. When I go to task manager, I can
see several Excel.exe tasks out there (even if I don't
have any Excel file open) and I have to close off those
tasks before I can open any excel file.

Any suggestions on how to resolve this issue?
Any help would be greatly appreciated.

thanks!


.
 

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

Top