release com proxy object in event handler

Sep 29, 2011 at 10:04 PM

Hello, Sir:

I am trying to free out com proxy objects in my project and find one issue i.e. each time when control goes to event handler, com proxy count increases by 1

say, when I click MyBtn,  control goes to MyBtnClick, upon entering the handler com proxy count increases by 1.

Next time, when I click it again, com proxy count increases again.

but I can't dispose the control b/c if I do,  the button will not response next time.

Wonder how do I get it right? thanks

        void MyBtnClick(CommandBarButton Ctrl, ref bool CancelDefault)
  //do sth.
            catch(Exception ex)

// can't use Ctrl.Dispose();

Sep 29, 2011 at 10:08 PM

i have problems again to understand. what means b/c ???
you means to dispose the button paramenter (CommandBarButton Ctrl) kills the event trigger?

Sep 29, 2011 at 10:29 PM

Sorry, b/c  - "because"

if I do Ctrl.Dispose() in handler, next time the  click event for the button will not be triggered.

Sep 29, 2011 at 10:40 PM

hmm i dont have these effect in all my addin projects.
whats up with your orginal button object? its still alive? 


Sep 29, 2011 at 11:26 PM
Edited Sep 30, 2011 at 2:44 PM

Sorry, Sebastian. Now it works fine even with Ctrl.Dispose() in handler.   I guess it probably was messed up by other stuff in my code. 

       void MyBtnClick(CommandBarButton Ctrl, ref bool CancelDefault)

//proxy count increases by 1

  //do sth.
            catch(Exception ex)

              Ctrl.Dispose(); ////proxy count decreases by 1


found the cause, another method(called by event handler) disposed button.

Oct 1, 2011 at 4:16 AM

Another question,  after I dispose all COM proxy objects after use.

When I debug MyAddIn in Visual Studio, I get an COMException at Range.Value2 = ...

but if I simply run it without debug,  everything works fine.

I am not sure what's wrong



Oct 1, 2011 at 2:03 PM

you dispose the range and use range.Value2 after them?
this is strictly prophited in COM.
have you a code snippet for me?


Oct 3, 2011 at 8:17 PM

Thanks, I will try to explain as  code base is not well organized and it scatters in several files. Will try to give you some snippet later.

MyAddIn has a button RefreshAll, once it is clicked, I will loop through cells on each worksheet and re-calculate all UDFs

those UDFs will call web service asynchronously and plot data in Excel in callback method.  During the process, there is a modal window displayed and users can cancel the process by clicking a cancel button on the modal window.

During the process, if I do not catch COMException in Visual studio, then everything works just fine.

If I go to Debug-> Exceptions->find COMException, then run it, I will get the following exception at

AttributeRange.Value2 = attr_data;

when the exception happens, I check AttributeRange.IsDisposed is false.  I googled and it seems it is about cross-thread issue.

See if you have any idea what's wrong? thanks a lot!


2011-10-03 13:36:41,612 [1] ERROR MyShared.Helper [(null)] - Name:COMException
Message:See inner exception for details.
Target:Void PropertySet(LateBindingApi.Core.COMObject, System.String, System.Object[])
Stack:   at LateBindingApi.Core.Invoker.PropertySet(COMObject comObject, String name, Object[] value)
   at NetOffice.ExcelApi.Range.set_Value2(Object value)
   at MyXLUIFunc.Models.VerticalTemplate.PlotSR_NonSeasonal()
   at MyXLUIFunc.Models.VerticalTemplate.PlotSR()
   at MyXLUIFunc.Models.VerticalTemplate.Plot()
   at MyXLUIFunc.Models.ReportBuilder.DataRequestJobFinished(DataRequestResponse response)

Message:Exception has been thrown by the target of an invocation.
Target:System.Object InvokeDispMethod(System.String, System.Reflection.BindingFlags, System.Object, System.Object[], Boolean[], Int32, System.String[])
Stack:   at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, CultureInfo culture)
   at LateBindingApi.Core.Invoker.PropertySet(COMObject comObject, String name, Object[] value)

Message:Exception from HRESULT: 0x800AC472

Oct 4, 2011 at 2:20 PM

okay this is not a problem about dipose. its a cross thread problem.
all your excel calls have to come from creator thread. are you sure you dont use another thread?

the following thread indicates its helpfull to create a message filter.

in NetOffice its very easy to do.

LateBindingApi.Core.Settings.EnableMessageFilter = true;

Oct 4, 2011 at 5:44 PM
Edited Oct 4, 2011 at 5:46 PM


I did this call Refresh on main thread, then on its callback, plot data in Excel.  I think and tracked the callback which is on main thread.

I put LateBindingApi.Core.Settings.EnableMessageFilter = true; but still get the same exception when debug.

If I do not debug,  or I do not catch COMException, then it works fine -- this is so weird. I found the issue accidentally. It makes me think maybe there are more unknown exceptions in code and I should try catching those in Visual Studio ->Debug->Exceptions -> Find

Oct 5, 2011 at 10:45 PM

your call comes from another thread. what you can do is to invoke your main thread and call excel from them.
this is not a NetOffice problem, moreover an Excel problem.

this topic helps:



Jan 6, 2012 at 9:35 PM


At last I created a sample project trying to reproduce the issue with no avail. 

Now the sample project shows a different issue. When you click Refresh All the first time in Excel session,  you will get some exception.  After that everything is fine.  Not sure if it is related to orginal issue I have. but maybe when you get a chance,  you can take a look and may find something related. thanks

Sample project is  @!195&parid=D4422FD0FCE557D3!119&authkey=!ADlak7hD9xmB0nc