This project has moved and is read-only. For the latest updates, please go here.

Cannot close excel.exe process

Feb 25, 2012 at 4:08 PM

Hi, I am trying to use NetOffice to write a excel UDF. This UDF will write value to some cells in spreadsheet. The basic function looks good.

However, if I use the _application.get_Range() method, excel.exe is still running in task manager after I close the excel. And if I comment out the get_Range() thing (marked as red in the following code), everything is good. 

Does anyone have some ideas?  Did I dispose the object inappropriately?  I appreciate your kindly help!

 

 public class ExcelUDFBase : IDTExtensibility2
    {
       
        /// <summary>
        /// Handle referencing back to the Excel session.
        /// </summary>
        protected MsExcel.Application MyExcelAppInstance;

       
        public virtual void OnConnection(object Application, ext_ConnectMode ConnectMode, object AddInInst, ref Array custom)
        {
            LateBindingApi.Core.Factory.Initialize();
            MyExcelAppInstance = new MsExcel.Application(null, Application);
        }
    }
public class TestNetOffice : ExcelUDFBase
    {
        private MsExcel.Range myRange;
        private object _locker = new object();

        public string NetOfficeTest()
        {
            lock (_locker)
            {
                LateBindingApi.Core.Factory.Initialize();

                myRange = (MsExcel.Range)MyExcelAppInstance.get_Caller(1);
                //MyExcelAppInstance.DisplayAlerts = false;
                MyExcelAppInstance.WorkbookBeforeCloseEvent += new Application_WorkbookBeforeCloseEventHandler(MyExcelAppInstance_WorkbookBeforeCloseEvent);
                var t = new Thread(dowork);
                t.SetApartmentState(System.Threading.ApartmentState.STA);
                t.Start();
                return myRange.get_Address(false, false, MsExcel.Enums.XlReferenceStyle.xlA1, Type.Missing, Type.Missing);
            }
        }

        private void dowork()
                {
                    try
                    {
                        myRange.get_Offset(1, 0).Value = 1;

                        MsExcel.Range dataRange = MyExcelAppInstance.get_Range("A1:B3"); // If Comment out this two lines, excel.exe will be killed;o.w. it is always there.
                        dataRange.Value = 2;
                    }
                    catch
                    { }
                }

        private void MyExcelAppInstance_WorkbookBeforeCloseEvent(Workbook Wb, ref bool Cancel)
        {

            try
            {
                MyExcelAppInstance.Quit();
                MyExcelAppInstance.Dispose();
                MyExcelAppInstance = null;
            }
            catch
            { 
            }
        }
   
 

Feb 25, 2012 at 5:57 PM

hello,

i know some issues exists with calling excel from a sperated thread with COM Interop generally.

1.) do you use Excel-DNA for UDF ? (recommended)
2.) it's possible, you send me a small test solution? (public.sebastian@web.de)  i do a test, check the proxy counts, etc. and give you an immediately response.
3.) i need more infos about your environment/ windows, excel, .net verson 32 or 64 bit.


Sebastian