Solved

How to ensure I don't keep keep creating additional EXCEL.EXE processes each time I call a routine in C# from a loop?

Posted on 2013-01-11
3
255 Views
Last Modified: 2013-01-14
I am developing a C# Windows application using VS2005.
In the following routine which I call from a loop,
it seems that I create a new EXECEL.EXE process each time this routine is called
according to the Windows Task Manager.
Is there a way to ensure that I drop the EXCEL.EXE before I call this routine the next time?

public static void ExcelFormat(string filename, string footnote)
{
            Excel.Application oXL = new Excel.Application();
            oXL.Visible = false;
            oXL.DisplayAlerts = false;

            Excel._Workbook oWB = (Excel._Workbook)oXL.Workbooks.Open(filename, 0, false, 5, Missing.Value, Missing.Value, false, Missing.Value, Missing.Value, false, false, Missing.Value, false, false, false);
            Excel._Worksheet oSheet = (Excel._Worksheet)oWB.ActiveSheet;
            oSheet.Activate();

            try
            {

                oSheet.get_Range(oSheet.Cells[2, 1], oSheet.Cells[65535, 1]).EntireColumn.NumberFormat = "#,##0";

            }
            finally
            {                
                oXL.Workbooks.Close();
                oXL.Quit();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oXL);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oWB);
                oSheet = null;
                oWB = null;
                oXL = null;
            }
}
0
Comment
Question by:zimmer9
3 Comments
 
LVL 23

Assisted Solution

by:Michael74
Michael74 earned 125 total points
Comment Utility
Instead of creating the excel object within the method you could create the object in the calling method and then pass this object to the method above. This  should prevent multiple objects being created. You can also call the garbage disposal directly to speed up the release of the memory.

Michael
0
 
LVL 9

Assisted Solution

by:sognoct
sognoct earned 125 total points
Comment Utility
int pid = 0;
Excel.Application oXL = new Excel.Application();
Process p = Process.GetProcessById(GetWindowThreadProcessId(oXL.Hwnd, out pid));

now that you have the pid of the process you can kill it according to your needs
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 250 total points
Comment Utility
Michael's remark about creating the Excel object only once is pretty much a golden rule in general. However, in my testing I hardly noticed any delay while creating it over and over so I left this alone for the moment, also because I don't know the structure of the rest of your application, mainly if indeed you are talking about a loop that is calling this function lots of times right after each other or maybe 3 times a day.

His remark about garbage collection I was going to suggest to stay away from however on my own system I couldn't get it to work without it...

Killing a process should be a last resort but in some cases a nice option to have.

What I ended up doing in the 'finally' block is:
oXL.Workbooks.Close();
oXL.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWB);
GC.Collect();
System.Runtime.InteropServices.Marshal.ReleaseComObject(oXL);

Open in new window

One more remark: I haven't used code like this a lot but somewhere I read that you shouldn't use temporary objects like you do with get_Range. This could be causing stray object references which cause the behaviour you're experiencing (not in general but specifically with COM Interop objects). So you would need another object to hold the range, set the NumberFormat on that, and release the range object as well in the finally block. However, for this particular call (since you use .EntireColumn) you should be able to just use:
oSheet.Columns[1].NumberFormat = "#,##0";

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now