473,326 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Excel application.quit leaves instance of Excel running

When you use Application.Quit() on an Excel application,
there can still be an instance of Excel running,
as seen in Task Manager.

You can try following the advice on MSDN:

http://support.microsoft.com/kb/Q317109

but this didn't solve the problem for me.

Instead, I used a crow-bar and did the following:

foreach (Process process in Process.GetProcessesByName("Excel")) {
process.Kill();
}

Process belongs to System.Diagnostics.

Nov 17 '05 #1
8 25934
See Inline
<Ch************@gmail.com> schrieb im Newsbeitrag
news:11**********************@g49g2000cwa.googlegr oups.com...
When you use Application.Quit() on an Excel application,
there can still be an instance of Excel running,
as seen in Task Manager.

You can try following the advice on MSDN:

http://support.microsoft.com/kb/Q317109

but this didn't solve the problem for me.
It works, but you have to call ReleaseComObject on every!!! excel object
your referencing.
Even on collection.
So instead of
Excel.Workbook workbook = oApp.Workbooks[0]:

you've got to use
Excel.Workbooks workbooks = oApp.Workbooks;
Excel Workbook workbook = workbooks[0];

And then call ReleaseCommObject on workbooks as well.

Instead, I used a crow-bar and did the following:

foreach (Process process in Process.GetProcessesByName("Excel")) {
process.Kill();
}

That's Dangerous.
You can't be sure the user is not running another Excel-session.

Christof
Nov 17 '05 #2
Christof,

could you explain why the following code doesn't kill the Excel
process:

Application application = new ApplicationClass();

try {
Workbooks books = application.Workbooks;
Workbook book = books.Open(BookPath, false, true,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);

application.DisplayAlerts = false;

XmlMaps maps = book.XmlMaps;
XmlMap dataMap = maps["Data_Map"];
dataMap.ImportXml(document.OuterXml, (object)true);
application.Calculate();
XmlMap outputMap = maps["Output_Map"];
outputMap.Export(outputPath, true);

Marshal.ReleaseComObject(outputMap);
outputMap = null;

Marshal.ReleaseComObject(dataMap);
dataMap = null;

Marshal.ReleaseComObject(maps);
maps = null;

Marshal.ReleaseComObject(book);
book = null;

Marshal.ReleaseComObject(books);
books = null;
}
finally {
application.Quit();
Marshal.ReleaseComObject(application);
application = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}

I can't see any objects I've referenced that I haven't released.

Thanks,

Chris.

Christof Nordiek wrote:
See Inline
<Ch************@gmail.com> schrieb im Newsbeitrag
news:11**********************@g49g2000cwa.googlegr oups.com...
When you use Application.Quit() on an Excel application,
there can still be an instance of Excel running,
as seen in Task Manager.

You can try following the advice on MSDN:

http://support.microsoft.com/kb/Q317109

but this didn't solve the problem for me.


It works, but you have to call ReleaseComObject on every!!! excel object
your referencing.
Even on collection.
So instead of
Excel.Workbook workbook = oApp.Workbooks[0]:

you've got to use
Excel.Workbooks workbooks = oApp.Workbooks;
Excel Workbook workbook = workbooks[0];

And then call ReleaseCommObject on workbooks as well.

Instead, I used a crow-bar and did the following:

foreach (Process process in Process.GetProcessesByName("Excel")) {
process.Kill();
}

That's Dangerous.
You can't be sure the user is not running another Excel-session.

Christof


Nov 17 '05 #3
By the way, there are no exceptions caught, so all the code in the try
clause runs.

Chris.

Nov 17 '05 #4
<Ch************@gmail.com> schrieb im Newsbeitrag
news:11**********************@g43g2000cwa.googlegr oups.com...
Christof,

could you explain why the following code doesn't kill the Excel
process:

Application application = new ApplicationClass();

try {
Workbooks books = application.Workbooks;
Workbook book = books.Open(BookPath, false, true,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);

application.DisplayAlerts = false;

XmlMaps maps = book.XmlMaps;
XmlMap dataMap = maps["Data_Map"];
dataMap.ImportXml(document.OuterXml, (object)true);
application.Calculate();
XmlMap outputMap = maps["Output_Map"];
outputMap.Export(outputPath, true);

Marshal.ReleaseComObject(outputMap);
outputMap = null;

Marshal.ReleaseComObject(dataMap);
dataMap = null;

Marshal.ReleaseComObject(maps);
maps = null;

Marshal.ReleaseComObject(book);
book = null;

Marshal.ReleaseComObject(books);
books = null;
}
finally {
application.Quit();
Marshal.ReleaseComObject(application);
application = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}

I can't see any objects I've referenced that I haven't released.


I can't see neither.
Maybe there is something strange about the XmlMap or XmlMaps wich I don't
know.
If your sure all ReleaseComObject were called i see know cause why Excel
isn't killed.
Nov 17 '05 #5
It is possible to use the Process.Kill method safely if you find all
the Excel processes
just before you create a new one, then find all the processes just
after you create the new one,
and compare them to find the one you've just created.

You could safely kill this one and leave the others unharmed, as long
as a new process isn't started
by the user at the same time as the code, e.g.:

Process[] originalProcesses = Process.GetProcessesByName("Excel");

Application application = new ApplicationClass();

Process[] newProcesses = Process.GetProcessesByName("Excel");

ArrayList list = new ArrayList();
foreach (Process newProcess in newProcesses) {
bool isNew = true;
foreach (Process originalProcess in originalProcesses) {
if (originalProcess.Id == newProcess.Id) {
isNew = false;
break;
}
}
if (isNew) list.Add(newProcess);
}

// Do your stuff

foreach (Process process in list) {
process.Kill();
}

This seems to work.

Nov 17 '05 #6
Ron
I went through this once. Try closing the workbook first:
book.Close(). In my experience, there is no need to call ReleaseComObject on
every Excel object, but you can play with that and see what works for you.

"Ch************@gmail.com" wrote:
Christof,

could you explain why the following code doesn't kill the Excel
process:

Application application = new ApplicationClass();

try {
Workbooks books = application.Workbooks;
Workbook book = books.Open(BookPath, false, true,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);

application.DisplayAlerts = false;

XmlMaps maps = book.XmlMaps;
XmlMap dataMap = maps["Data_Map"];
dataMap.ImportXml(document.OuterXml, (object)true);
application.Calculate();
XmlMap outputMap = maps["Output_Map"];
outputMap.Export(outputPath, true);

Marshal.ReleaseComObject(outputMap);
outputMap = null;

Marshal.ReleaseComObject(dataMap);
dataMap = null;

Marshal.ReleaseComObject(maps);
maps = null;

Marshal.ReleaseComObject(book);
book = null;

Marshal.ReleaseComObject(books);
books = null;
}
finally {
application.Quit();
Marshal.ReleaseComObject(application);
application = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}

I can't see any objects I've referenced that I haven't released.

Thanks,

Chris.

Christof Nordiek wrote:
See Inline
<Ch************@gmail.com> schrieb im Newsbeitrag
news:11**********************@g49g2000cwa.googlegr oups.com...
When you use Application.Quit() on an Excel application,
there can still be an instance of Excel running,
as seen in Task Manager.

You can try following the advice on MSDN:

http://support.microsoft.com/kb/Q317109

but this didn't solve the problem for me.


It works, but you have to call ReleaseComObject on every!!! excel object
your referencing.
Even on collection.
So instead of
Excel.Workbook workbook = oApp.Workbooks[0]:

you've got to use
Excel.Workbooks workbooks = oApp.Workbooks;
Excel Workbook workbook = workbooks[0];

And then call ReleaseCommObject on workbooks as well.

Instead, I used a crow-bar and did the following:

foreach (Process process in Process.GetProcessesByName("Excel")) {
process.Kill();
}

That's Dangerous.
You can't be sure the user is not running another Excel-session.

Christof


Nov 17 '05 #7
<Ch************@gmail.com> schrieb im Newsbeitrag
news:11**********************@g44g2000cwa.googlegr oups.com...
It is possible to use the Process.Kill method safely if you find all
the Excel processes
just before you create a new one, then find all the processes just
after you create the new one,
and compare them to find the one you've just created.

You could safely kill this one and leave the others unharmed, as long
as a new process isn't started
by the user at the same time as the code, e.g.:

Process[] originalProcesses = Process.GetProcessesByName("Excel");

Application application = new ApplicationClass();

Process[] newProcesses = Process.GetProcessesByName("Excel");

ArrayList list = new ArrayList();
foreach (Process newProcess in newProcesses) {
bool isNew = true;
foreach (Process originalProcess in originalProcesses) {
if (originalProcess.Id == newProcess.Id) {
isNew = false;
break;
}
}
if (isNew) list.Add(newProcess);
}

// Do your stuff

foreach (Process process in list) {
process.Kill();
}

This seems to work.

Looks to be rather safe.
Whit a very unlikly chance, that there could be started another instance of
Excel
just between the two GetProcessesByName calls. Very very unlikly and easy to
detect. The list will contain more than one process.
Not even sure if it was worth to mention that.
Nov 17 '05 #8
Does the code in the finalize function finish???
If you call the GC.WaitForPendingFinalizers(); it waits syncronously
for the finalization queue to empty...... (all the objects which have
finalization methods)

If the code in the finalization section does not finish the process
will not terminate and just hang.
Has finalization on all you're objects completed? Check that you're
function terminates.

Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Otie | last post by:
I found the following under the GetObject help notes and in the example for GetObject: "This example uses the GetObject function to get a reference to a specific Microsoft Excel worksheet...
8
by: mytfein | last post by:
Hi Everyone, Background: Another department intends to ftp a .txt file from the mainframe, for me to process. The objective is to write a vb script that would be scheduled to run daily to...
7
by: taylor.bryant | last post by:
I am running: Win XP SP2 Excel 2002, Access 2002 (Office XP SP3) Using Visual Basic (not VB.NET) At one point (prior to XP SP2?!? - I can't pin it down), this did not happen and I was easily...
5
by: Wenke Ji | last post by:
Hi I open a Excel workbook using below API: Set ExcelServer = CreateObject("EXCEL.Application") Set TargetWorkbook = ExcelServer.Workbooks.Open (CurrentBook) Befor the programm exit , I use...
7
by: SteveS | last post by:
I'm attempting to close EXCEL from within my VB.NET application. Using the excel object library to write data to my spreadsheet is working fine but when I try to quit application object it does not...
16
by: alexia.bee | last post by:
Hi all, In some weird reason, excel instance won;t die if i remove the comment from 4 lines of setting values into struct. here is a snipcode public...
9
by: Doug Glancy | last post by:
I got the following code from Francesco Balena's site, for disposing of Com objects: Sub SetNothing(Of T)(ByRef obj As T) ' Dispose of the object if possible If obj IsNot Nothing AndAlso...
6
by: pleaseexplaintome_2 | last post by:
Help please. The excel instance is removed from task manager when I run the code below. If I uncomment the lines pertaining to a workbook, the instance of excel is not removed from task manager. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.