473,385 Members | 1,461 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,385 software developers and data experts.

EXCEL.EXE remains in processes after Dispose...

Scenario:
The end-user selects File-Save from my applications (WinForm) menu, specifies a filename, and waits waits for my app to write the contents of a dataset to an Excel workbook. The SaveXLReport method completes successfully, calls myXLClass.Dispose, and returns control to the user form at a base state.

At this point, there is still an EXCEL.EXE in processes and the specified workbook cannot be saved until that process is ended from Task Manager or until the end-user exits my application, which also ends the EXCEL.EXE process.

Here are relevant snippets from myXL:

public class MyXL : System.IDisposable
{
private excel9.Application xlapp;

public MyXL()
{
xlapp = null;
}

~MyXL()
{
if (xlapp != null)
Dispose(false);
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

public virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
this.xlapp.Quit();
int referenceCount = 0;
do
{
referenceCount = System.Runtime.InteropServices.Marshal.ReleaseComO bject(this.xlapp);
}
while (referenceCount > 0);

this.xlapp = null;
if (disposing)
{
GC.SuppressFinalize(this);
}
}
disposed = true;
}
}

Here is the SaveXLReport method from the Report class, which is a windows form:

private bool SaveXLReport(DataSet ds)
{
bool failed = true;
string filename = ds.ExtendedProperties["ReportName"].ToString();
for (int i = 0; i < ds.Tables.Count; i++)
UpdateXLWorksheetData(filename, ds.Tables[i]);
if (cymerXL != null)
{
cymerXL.SaveOpenWorkbook();
cymerXL.Dispose();
failed = false;
}
return failed;
}
Nov 16 '05 #1
3 9932
I think this is a bug in the COM for Excel.

I ran into the exact same problem doing reporting from a simple Access
database into a complex Excel spreadsheet.

I literally spent a couple of days trying to solve it, without any luck.

Vagabond Software wrote:
Scenario:
The end-user selects File-Save from my applications (WinForm) menu, specifies a filename, and waits waits for my app to write the contents of a dataset to an Excel workbook. The SaveXLReport method completes successfully, calls myXLClass.Dispose, and returns control to the user form at a base state.

At this point, there is still an EXCEL.EXE in processes and the specified workbook cannot be saved until that process is ended from Task Manager or until the end-user exits my application, which also ends the EXCEL.EXE process.

Here are relevant snippets from myXL:

public class MyXL : System.IDisposable
{
private excel9.Application xlapp;

public MyXL()
{
xlapp = null;
}

~MyXL()
{
if (xlapp != null)
Dispose(false);
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

public virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
this.xlapp.Quit();
int referenceCount = 0;
do
{
referenceCount = System.Runtime.InteropServices.Marshal.ReleaseComO bject(this.xlapp);
}
while (referenceCount > 0);

this.xlapp = null;
if (disposing)
{
GC.SuppressFinalize(this);
}
}
disposed = true;
}
}

Here is the SaveXLReport method from the Report class, which is a windows form:

private bool SaveXLReport(DataSet ds)
{
bool failed = true;
string filename = ds.ExtendedProperties["ReportName"].ToString();
for (int i = 0; i < ds.Tables.Count; i++)
UpdateXLWorksheetData(filename, ds.Tables[i]);
if (cymerXL != null)
{
cymerXL.SaveOpenWorkbook();
cymerXL.Dispose();
failed = false;
}
return failed;
}


--
Colin R. R. Johnson
C&J Solutions
- Remember, everything you see on screen is but ones and zeroes.
Nov 16 '05 #2
I was fortunate enough to resolve this problem this morning.

I created an ArrayList to store every Excel object that is created and use my Dispose method to iterate through the ArrayList and get rid of all Excel objects. I also added a GC.Collect call after the Dispose call in the Reporter class. Here is the new code in the Dispose method of my Excel class.

for (int i = 0; i < alXlObjects.Count; i++)
{
int referenceCount = 0;
do
{
referenceCount = System.Runtime.InteropServices.Marshal.ReleaseComO bject(alXlObjects[i]);
alXlObjects[i] = null;
}
while (referenceCount > 0);
}

Thanks for the reply.

carl
"Colin R. R. Johnson" <cj@andjsolutions.com> wrote in message news:wk*********************@news20.bellglobal.com ...
I think this is a bug in the COM for Excel.

I ran into the exact same problem doing reporting from a simple Access
database into a complex Excel spreadsheet.

I literally spent a couple of days trying to solve it, without any luck.

Vagabond Software wrote:
Scenario:
The end-user selects File-Save from my applications (WinForm) menu, specifies a filename, and waits waits for my app to write the contents of a dataset to an Excel workbook. The SaveXLReport method completes successfully, calls myXLClass.Dispose, and returns control to the user form at a base state.

At this point, there is still an EXCEL.EXE in processes and the specified workbook cannot be saved until that process is ended from Task Manager or until the end-user exits my application, which also ends the EXCEL.EXE process.

Here are relevant snippets from myXL:

public class MyXL : System.IDisposable
{
private excel9.Application xlapp;

public MyXL()
{
xlapp = null;
}

~MyXL()
{
if (xlapp != null)
Dispose(false);
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

public virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
this.xlapp.Quit();
int referenceCount = 0;
do
{
referenceCount = System.Runtime.InteropServices.Marshal.ReleaseComO bject(this.xlapp);
}
while (referenceCount > 0);

this.xlapp = null;
if (disposing)
{
GC.SuppressFinalize(this);
}
}
disposed = true;
}
}

Here is the SaveXLReport method from the Report class, which is a windows form:

private bool SaveXLReport(DataSet ds)
{
bool failed = true;
string filename = ds.ExtendedProperties["ReportName"].ToString();
for (int i = 0; i < ds.Tables.Count; i++)
UpdateXLWorksheetData(filename, ds.Tables[i]);
if (cymerXL != null)
{
cymerXL.SaveOpenWorkbook();
cymerXL.Dispose();
failed = false;
}
return failed;
}


--
Colin R. R. Johnson
C&J Solutions
- Remember, everything you see on screen is but ones and zeroes.

Nov 16 '05 #3
I would just like to add this...

There is a document somewhere in the MSDN clarifying the COM interop. I
don't remember where exactly it was, but it says something about the runtime
creating only 1 reference to each object through a proxy, and reusing this
one afterwards. After some experiments, the simplest solution I found is
what I've posted below (And it's the ONLY case so far where I found it
useful to call the GC.Collect() explicitly). Keeping track of the objects
that are to be disposed is pretty hard, and after all, it might not work
well if you for example want to leave the excel open after doing whatever
you want to do. The GC.Collect() ways is pretty clean, as you really don't
have to care about any reference counting.

void DoSomethingWithExcel()
{
// do all your automation only in this method, leaving no references
around
..
...
...
}

void DoItNow()
{
DoSomethingWithExcel();
GC.Collect();
}

This way, you don't even need to bother about calling dispose, etc.

HTH,
Stefan

"Vagabond Software" <carlfenley-X-@-X-san.rr.com> wrote in message
news:uR**************@TK2MSFTNGP10.phx.gbl...
I was fortunate enough to resolve this problem this morning.

I created an ArrayList to store every Excel object that is created and use
my Dispose method to iterate through the ArrayList and get rid of all Excel
objects. I also added a GC.Collect call after the Dispose call in the
Reporter class. Here is the new code in the Dispose method of my Excel
class.

for (int i = 0; i < alXlObjects.Count; i++)
{
int referenceCount = 0;
do
{
referenceCount =
System.Runtime.InteropServices.Marshal.ReleaseComO bject(alXlObjects[i]);
alXlObjects[i] = null;
}
while (referenceCount > 0);
}

Thanks for the reply.

carl
"Colin R. R. Johnson" <cj@andjsolutions.com> wrote in message
news:wk*********************@news20.bellglobal.com ...
I think this is a bug in the COM for Excel.

I ran into the exact same problem doing reporting from a simple Access
database into a complex Excel spreadsheet.

I literally spent a couple of days trying to solve it, without any luck.

Vagabond Software wrote:
Scenario:
The end-user selects File-Save from my applications (WinForm) menu,
specifies a filename, and waits waits for my app to write the contents
of a dataset to an Excel workbook. The SaveXLReport method completes
successfully, calls myXLClass.Dispose, and returns control to the user
form at a base state.

At this point, there is still an EXCEL.EXE in processes and the
specified workbook cannot be saved until that process is ended from Task
Manager or until the end-user exits my application, which also ends the
EXCEL.EXE process.

Here are relevant snippets from myXL:

public class MyXL : System.IDisposable
{
private excel9.Application xlapp;

public MyXL()
{
xlapp = null;
}

~MyXL()
{
if (xlapp != null)
Dispose(false);
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

public virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
this.xlapp.Quit();
int referenceCount = 0;
do
{
referenceCount =
System.Runtime.InteropServices.Marshal.ReleaseComO bject(this.xlapp);
}
while (referenceCount > 0);

this.xlapp = null;
if (disposing)
{
GC.SuppressFinalize(this);
}
}
disposed = true;
}
}

Here is the SaveXLReport method from the Report class, which is a
windows form:

private bool SaveXLReport(DataSet ds)
{
bool failed = true;
string filename = ds.ExtendedProperties["ReportName"].ToString();
for (int i = 0; i < ds.Tables.Count; i++)
UpdateXLWorksheetData(filename, ds.Tables[i]);
if (cymerXL != null)
{
cymerXL.SaveOpenWorkbook();
cymerXL.Dispose();
failed = false;
}
return failed;
}


--
Colin R. R. Johnson
C&J Solutions
- Remember, everything you see on screen is but ones and zeroes.

Nov 16 '05 #4

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

Similar topics

11
by: Tim Marsden | last post by:
Hi, I have a routine which is call from a ASP.NET web form. This routine creates an excel application, opens a workbook , runs some code to update the workbook, saves it as HTML on the sever and...
10
by: Lars-Erik Aabech | last post by:
Hi! This issue have been discussed a lot, but I haven't found a solution that applies to ASP.NET. I have a library which does some operations on Excel documents, and it will be used in an...
22
by: Howard Kaikow | last post by:
There's a significant problem in automating Excel from VB .NET. Reminds me of a problem I encountered almost 3 years ago that was caused by the Norton Auntie Virus Office plug-in. Can anybody...
16
by: LP | last post by:
Hello, I am trying to use .NET with Excel. I installed Office 2003 and selected ..NET programming suport option, so it installed all those PIA, as MS sugests. But I can not find a way to destroy...
3
by: pleaseexplaintome_2 | last post by:
using the code below (some parts not included), I create a new excel workbook with spreadheets. I then want to delete a spreadsheet, but a reference remains open and excel stays in task manager...
10
by: Ben | last post by:
Hi, i have a weird problem and i don't know who is responsible for this: IIS, excel or asp.net. My problem: we use an asp.net 2.0 application under IIS 6.0 (server 2003 sp2) which must write...
9
by: fahadqureshi | last post by:
I am running two visual basic modules in Access and keep coming across an annoying problem. Both the vb modules transfer an excel spreadsheet to an access database but for some reason after the...
7
by: =?Utf-8?B?VGVycnkgSG9sbGFuZA==?= | last post by:
I have a vb.net app that opens an excel worksheet, reads data and then closes the sheet. Im noticing that the Excel process is still running after I have closed and disposed of my excel objects. ...
14
by: John | last post by:
Hi I have a winform app. When this app is closed it remains under 'Processes' in Task Manager. What is the problem and how can I ensure that app closes completely and releases all resources? ...
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...
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
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.