Excel Kill process

G

Guest

Hi to all,

I have a windows application that uses workbooks, sheets, ranges, etc...

This application is consumed by few clients.

Some weeks ago I had problems to kill excel.exe process but i solve with
this sentence:

System.Runtime.InteropServices.Marshal.ReleaseComObject(o);

where o is a com object (excel sheet, or workbook, etc...).

Now i start to work with Charts in excel and i used the object ChartObject
to get the chart that im interested. The case is that using the
ReleaseComObject the excel process is not killed, and i dont know if this
ChartObject instances more Com objects that i have to release.

Anybody knows how could i solve it.

Any help will be grateful.
Thanks in advance.
Josema.


Thanks
Regards.
Josema
 
W

Wessel Troost

to get the chart that im interested. The case is that using the
ReleaseComObject the excel process is not killed, and i dont know if this
ChartObject instances more Com objects that i have to release.
By the COM rules you only have to release instances that you instantiated
yourself. So if ChartObject creates some new interfaces, you don't have to
worry about them.

Check if your code doesn't accidentally "lose" an interface somewhere
(which is easy to do by accident) and if you close objects in the right
order (first chart, then workbook, then Excel.)

Greetings,
Wessel
 
G

Guest

Hi Wessel, first of all thanks for your fast response.

i cant find why the process is not killed, Here you have the code, but i
dont understand why, if i dont use the line where i call to Export method,
the process is killed correctly, but if i use it, the process is not killed...

//init
Microsoft.Office.Interop.Excel.Application app = null;
Workbook book = null;
Worksheet sheet = null;
Chart chartobject=null;
//

//process
book = app.Workbooks.Open(excelFilePath, Missing.Value, Missing.Value,
Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value);

sheet = (Worksheet)book.Worksheets[1];

chartobject=((ChartObject)sheet.ChartObjects(1)).Chart;

bool var=chartobject.Export("C:\\Test.gif","GIF",false);

ReleaseComObject(chartobject);
ReleaseComObject(sheet);
try
{
if (book != null)
{
book.Close(false, Missing.Value, Missing.Value);
}
}
catch{}
ReleaseComObject(book);
try
{
if (app != null)
{
app.Quit();
}
}
catch{}
ReleaseComObject(app);
GC.Collect();
GC.WaitForPendingFinalizers();

//
 
W

Wessel Troost

i cant find why the process is not killed, Here you have the code, but i
dont understand why, if i dont use the line where i call to Export
method,
the process is killed correctly, but if i use it, the process is not
killed...
That's strange, it doesn't look like the Chart.Export() method returns
anything you have to free. Maybe Excel is doing some background
processing during Export... Try to wait a few seconds after calling
Export, and see if Excel closes nicely if you do?

That's not something you'd like to do in production code, though...

Greetings,
Wessel

//init
Microsoft.Office.Interop.Excel.Application app = null;
Workbook book = null;
Worksheet sheet = null;
Chart chartobject=null;
//

//process
book = app.Workbooks.Open(excelFilePath, Missing.Value, Missing.Value,
Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value);

sheet = (Worksheet)book.Worksheets[1];

chartobject=((ChartObject)sheet.ChartObjects(1)).Chart;

bool var=chartobject.Export("C:\\Test.gif","GIF",false);

ReleaseComObject(chartobject);
ReleaseComObject(sheet);
try
{
if (book != null)
{
book.Close(false, Missing.Value, Missing.Value);
}
}
catch{}
ReleaseComObject(book);
try
{
if (app != null)
{
app.Quit();
}
}
catch{}
ReleaseComObject(app);
GC.Collect();
GC.WaitForPendingFinalizers();

//
 
J

Josema

Hi Again,

Thanks for you fast response.

At the end, a solution is finded...

When we use the export method, and you add code after this export method
this code is not correctly executed in the time that has to be executed.
To avoid is is very simple...

//First you have the export method

Chart.Export(.........)

//and then call a method

this.MethodToCall()

//and then call to release the objects
this.Release(object o)

I dont know why but using this way, first Makes the Export, and after
finish the export go to the another method and executes the code that is
inside...

I supposed that the export method is executed like an Asyncronous
method. but if you call to a method after the line Export(), first
finish the Export, and then executes the method... after that if you
releases the com objects, it will work... but if you dont call to a
method after the Export() method, first will execute the Export() then
before finish the execution of it will be execute the lines after, and
the process will be not killed...

Thanks a lot.
Regards.
Josema.
 

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