473,566 Members | 3,004 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

excel process not terminating properly

hello all you gurus. I am struggling with releasing com objects. I
have isolated the problem to the code below. Objects are released and
the process ends until I
use "int k = sheet.Count;" Then the process does not end. So I feel
confident the problem occurrs here. It appears another reference is
created that needs to be closed. Can anyone tell me how to do
this? :)
Thank you

Excel.Workbook workbook =
(Excel.Workbook )excelapplicati on.ActiveWorkbo ok;
Excel.Sheets sheet = workbook.Worksh eets;

// problem here
int k = sheet.Count;

System.Runtime. InteropServices .Marshal.Releas eComObject(shee t);
sheet = null;
System.Runtime. InteropServices .Marshal.Releas eComObject(work book);
workbook = null;

Apr 18 '07 #1
13 1176
Try adding the following just BEFORE your call ReleaseCOMObjec t on them:

sheet = null;
workbook = null;

<ch**********@h otmail.comwrote in message
news:11******** **************@ y80g2000hsf.goo glegroups.com.. .
hello all you gurus. I am struggling with releasing com objects. I
have isolated the problem to the code below. Objects are released and
the process ends until I
use "int k = sheet.Count;" Then the process does not end. So I feel
confident the problem occurrs here. It appears another reference is
created that needs to be closed. Can anyone tell me how to do
this? :)
Thank you

Excel.Workbook workbook =
(Excel.Workbook )excelapplicati on.ActiveWorkbo ok;
Excel.Sheets sheet = workbook.Worksh eets;

// problem here
int k = sheet.Count;

System.Runtime. InteropServices .Marshal.Releas eComObject(shee t);
sheet = null;
System.Runtime. InteropServices .Marshal.Releas eComObject(work book);
workbook = null;

Apr 18 '07 #2
"Scott M." <s-***@nospam.nosp amschrieb:
Try adding the following just BEFORE your call ReleaseCOMObjec t on them:

sheet = null;
workbook = null;
Bad idea, because 'ReleaseComObje ct' won't release the objects if a null
reference is passed to it.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Apr 18 '07 #3
<ch**********@h otmail.comschri eb:
I am struggling with releasing com objects. I
have isolated the problem to the code below. Objects are released and
the process ends until I
use "int k = sheet.Count;" Then the process does not end.
PRB: Office Application Does Not Quit After Automation from Visual Studio
..NET Client
<URL:http://support.microso ft.com/?scid=kb;EN-US;317109>
-"Troubleshootin g"

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Apr 18 '07 #4
Your link shows (in a more elaborate way) how to do what the OP is already
doing (RleaseCOMObjec t and set to null). Is there a particular part of the
article that you suggest?
"Herfried K. Wagner [MVP]" <hi************ ***@gmx.atwrote in message
news:uF******** ******@TK2MSFTN GP06.phx.gbl...
<ch**********@h otmail.comschri eb:
>I am struggling with releasing com objects. I
have isolated the problem to the code below. Objects are released and
the process ends until I
use "int k = sheet.Count;" Then the process does not end.

PRB: Office Application Does Not Quit After Automation from Visual Studio
.NET Client
<URL:http://support.microso ft.com/?scid=kb;EN-US;317109>
-"Troubleshootin g"

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Apr 18 '07 #5
"Scott M." <s-***@nospam.nosp amschrieb:
Your link shows (in a more elaborate way) how to do what the OP is already
doing (RleaseCOMObjec t and set to null). Is there a particular part of
the article that you suggest?
I suggest the "more elaborate way".

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Apr 18 '07 #6
On Apr 18, 3:48 pm, "Herfried K. Wagner [MVP]" <hirf-spam-me-
h...@gmx.atwrot e:
<chuckie_9...@h otmail.comschri eb:
I am struggling with releasing com objects. I
have isolated the problem to the code below. Objects are released and
the process ends until I
use "int k = sheet.Count;" Then the process does not end.

PRB: Office Application Does Not Quit After Automation from Visual Studio
.NET Client
<URL:http://support.microso ft.com/?scid=kb;EN-US;317109>
-"Troubleshootin g"

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
thank you for your resposnse and I have to admit I am somewhat
confused (I guess that's obvious). I read the article and maybe I
missed the obvious, but I do use GC.Collect() and
GC.WaitForPendi ngFinalizers() and excelapplicatio n.quit(). And I
think I understand that I must explicitly reference any object that is
implicitly created. So, I think (I am probably wrong) int k =
sheet.Count; creates an implicit reference that I must explicitly
reference so I can remove the reference. Is this correct? If so , how
do I do that? If I am wrong, please correct me. Everything works
fine until this statement is added. Thank you

Apr 18 '07 #7
On Apr 18, 3:48 pm, "Herfried K. Wagner [MVP]" <hirf-spam-me-
h...@gmx.atwrot e:
<chuckie_9...@h otmail.comschri eb:
I am struggling with releasing com objects. I
have isolated the problem to the code below. Objects are released and
the process ends until I
use "int k = sheet.Count;" Then the process does not end.

PRB: Office Application Does Not Quit After Automation from Visual Studio
.NET Client
<URL:http://support.microso ft.com/?scid=kb;EN-US;317109>
-"Troubleshootin g"

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
thank you for your resposnse and I have to admit I am somewhat
confused (I guess that's obvious). I read the article and maybe I
missed the obvious, but I do use GC.Collect() and
GC.WaitForPendi ngFinalizers() and excelapplicatio n.quit(). And I
think I understand that I must explicitly reference any object that is
implicitly created. So, I think (I am probably wrong) int k =
sheet.Count; creates an implicit reference that I must explicitly
reference so I can remove the reference. Is this correct? If so , how
do I do that? If I am wrong, please correct me. Everything works
fine until this statement is added. Thank you

Apr 18 '07 #8
<ch**********@h otmail.comwrote in message
news:11******** **************@ b58g2000hsg.goo glegroups.com.. .
On Apr 18, 3:48 pm, "Herfried K. Wagner [MVP]" <hirf-spam-me-
h...@gmx.atwrot e:
><chuckie_9...@ hotmail.comschr ieb:
I am struggling with releasing com objects. I
have isolated the problem to the code below. Objects are released and
the process ends until I
use "int k = sheet.Count;" Then the process does not end.

PRB: Office Application Does Not Quit After Automation from Visual Studio
.NET Client
<URL:http://support.microso ft.com/?scid=kb;EN-US;317109>
-"Troubleshootin g"

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

thank you for your resposnse and I have to admit I am somewhat
confused (I guess that's obvious). I read the article and maybe I
missed the obvious, but I do use GC.Collect() and
GC.WaitForPendi ngFinalizers() and excelapplicatio n.quit(). And I
think I understand that I must explicitly reference any object that is
implicitly created. So, I think (I am probably wrong) int k =
sheet.Count; creates an implicit reference that I must explicitly
reference so I can remove the reference. Is this correct? If so , how
do I do that? If I am wrong, please correct me. Everything works
fine until this statement is added. Thank you

No, Count does not create an reference to a COM object, sheet holds a reference to the COM
interface.
But as long as you don't post a *complete* sample, that illustrates the issue, you won't get
any sensible answers.

A complete sample looks something like this:

using System;
using System.Collecti ons.Generic;
using System.Text;
using System.Globaliz ation;
using System.Reflecti on;

using Exl = Microsoft.Offic e.Interop.Excel ;
namespace OffExc
{
class Program
{
[STAThread]
static void Main(string[] args)
{
System.Threadin g.Thread.Curren tThread.Current Culture = new CultureInfo( "en-US",
false );
Exl.Application exApp = new Microsoft.Offic e.Interop.Excel .ApplicationCla ss();
// Reference 1
Exl.Workbook wb = exApp.Workbooks .Add(Missing.Va lue); // Reference 2
exApp.Visible = true;
// Keep Excel visible for a while..
System.Threadin g.Thread.Sleep( 3000);
Exl.Sheets sheet = wb.Worksheets; // // Reference 3
int k = sheet.Count;
// Quit
exApp.Quit();
// Release the three COM references...
System.Runtime. InteropServices .Marshal.Releas eComObject(shee t);
System.Runtime. InteropServices .Marshal.Releas eComObject(wb);
System.Runtime. InteropServices .Marshal.Releas eComObject(exAp p);
GC.Collect();
// Let the finalizer run, this one will delete the unmanaged COM wrappers,
// if for one or another reason, the finalizer cannot run to completion,
// chances are that the Excel process won't get removed !!!!!!!!
GC.WaitForPendi ngFinalizers();
// Excel should be gone by now... keep the console processrunning for a while
System.Threadin g.Thread.Sleep( 30000);
}
}
}

And above code works as expected on my box.

Willy.

Apr 18 '07 #9
this is a scam

the problem is using DAO. screw DAO and you won't have this problem

ADO works like a charm


On Apr 18, 12:48 pm, "Herfried K. Wagner [MVP]" <hirf-spam-me-
h...@gmx.atwrot e:
<chuckie_9...@h otmail.comschri eb:
I am struggling with releasing com objects. I
have isolated the problem to the code below. Objects are released and
the process ends until I
use "int k = sheet.Count;" Then the process does not end.

PRB: Office Application Does Not Quit After Automation from Visual Studio
.NET Client
<URL:http://support.microso ft.com/?scid=kb;EN-US;317109>
-"Troubleshootin g"

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Apr 18 '07 #10

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

Similar topics

3
8363
by: David Berman | last post by:
Hi, I've written an application to do a while bunch of Excel automation. I open a file, scan through all the worksheets, extract data, and then I try to close Excel. However, I can see that I'm not doing it effectively. If I am debugging my app, and I kill the app before my app exits, but after the call that should close excel, I still have...
2
402
by: Mark | last post by:
Hi all, as many had, i have some problems terminating the excel-process. I implemented the solutions i found in this forums. Tried most combinations of them ;-). It still doesn't work. I'll post my code-snippets below. I hope u can shine some light on the solution.
2
21912
by: Powerguy | last post by:
Hi all, I am looking for a way to get the Process id (or a handle) of an EXCEL process created from within my code. For example when the following code is executed: Dim EXL As Excel.Application = New Excel.Application a new instance of EXCEL.EXE is created in the task manager.
18
3078
by: lgbjr | last post by:
Hi All, I have a VB.NET app that, among other things, writes data to Excel. I am having trouble getting the Excel process to terminate after I quit Excel. I found an article related to this problem: http://support.microsoft.com/default.aspx?scid=kb;en-us;317109 Below is a sample of code that I wrote based on the above article. The excel...
13
2992
by: chuckie_9497 | last post by:
hello all you gurus. I am struggling with releasing com objects. I have isolated the problem to the code below. Objects are released and the process ends until I use "int k = sheet.Count;" Then the process does not end. So I feel confident the problem occurrs here. It appears another reference is created that needs to be closed. Can...
0
1294
by: ravindarjobs | last post by:
dear friends, i am using ms access 2003. i wanted to export data from access table to excel table. so i have followed this Dim db As Database Dim ea As Excel.Application Dim rs As Recordset
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7893
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8109
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6263
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.