Any issue when I reference Excel Object Model in a separate thread?

Aug 14, 2014 at 4:04 PM
My addin has a refresh all button. When it is clicked, I will look through worksheet and look for methods (defined in my addin) and refresh data for all these methods, this is done in a separate thread, now in main thread, I show a modal dialog window with "Cancel" button on it.
When refresh is done, the modal window closes.
Everything works fine.
Except when I close Excel, Excel.exe stays in task manage.
http://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects
I think maybe at least one member of a COM object is not released
So I track down my refresh method on the separate thread, the proxy count when entering the method and exiting are the same. So I guess there is no memory leak and all COM object are released properly. but I do not understand why Excel.exe stays

Sorry not sure if I make it clear

Thanks
Aug 14, 2014 at 4:28 PM
Here is code I have
public partial class XLWindow : Window
{
    public XLWindow()
    {
        InitializeComponent();
    } 
    private void UserControlLoaded(object sender, RoutedEventArgs e)
    {
        model = DataContext as MainViewModel;
        if (model != null)
        {
            if(RefreshTrehad != null && RefreshTrehad.IsAlive) 
            {
                RefreshTrehad.Abort();  //only allow one refresh thread at a time
                RefreshTrehad = null;
            }
            RefreshTrehad = new Thread(
                () =>
                {
                    if (model.RefreshMode == Helper.RefreshMode.RefreshOneWorkbook)
                    {
                        model.RefreshAll(this);
                    }
                    else if (model.RefreshMode == Helper.RefreshMode.Refresh)
                    {
                         model.Refresh(this);
                    }
                }
            );
            RefreshTrehad.Start();
        }
    }

    private void DoRefreshWork(MainViewModel model)
    {
        //loop through each worksheet, find method,  call web service to get data asychronously
        //when data comes back, plot data in Excel
    }
}


        //call refresh 
        private void RefreshAll()
        {
                var mainVM = MainViewModel;
                var xlWindow = new XLWindow();                  
                xlWindow.DataContext = mainVM;
                bool? successNullable = xlWindow.ShowDialog();
                RefreshTrehad.Join();
                mainVM.Dispose();
        }       

        public class MainViewModel
        {
            ....
            public void Dispose()
            {            
                if (Cells != null) Cells.Dispose();
                if (Rng != null) Rng.Dispose();
                if (Sheet != null) Sheet.Dispose();
                if (Sheets != null) Sheets.Dispose();
                if (Workbook!=null) Workbook.Dispose();
                if (Workbooks != null) Workbooks.Dispose();
                XLApp = null;
            }
        }
Aug 15, 2014 at 5:22 PM
I created a sample project to show the issue @ http://1drv.ms/1phJSag
Please take a look when you get a chance,

Thanks