V
Vagabond Software
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.ReleaseComObject(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);
if (cymerXL != null)
{
cymerXL.SaveOpenWorkbook();
cymerXL.Dispose();
failed = false;
}
return failed;
}
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.ReleaseComObject(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);
if (cymerXL != null)
{
cymerXL.SaveOpenWorkbook();
cymerXL.Dispose();
failed = false;
}
return failed;
}