English 中文(简体)
具体目标 处理COM时的例外情况 活动
原标题:TargetInvocationException when handling a COM Event

我正在开发一个COM dll图书馆,我仅用于测试。

我在申请中宣布反对。

Private m_VarName As MyLib.CMyComClass

迄今为止,情况良好。

但是,现在我需要一个活动来通报某些事情的适用情况,因此,我在Cat dll实施这种活动,并将声明变成了。

私人 活动m_VarName As MyLib.CMyComClass

至今,情况良好。 但是,如果我增加一个子来处理我的活动:

Private Sub m_VarName_OnCaptureStop() 残疾人 m_VarName.MyEvent

......

结尾处

我首次制造了这个物体,但没有任何坏事发生,但如果我重新证实它的话。

如果(无)

m:VarName=New MyLib.CMyComClass

然后,我收到一条似乎与反思有关的加密目标“职业外观”(FAIK,我没有使用)。

If I remove the "Handles m_VarName.MyEvent" part, everything seems to work. In case it matters, I am not firing any event, for now. Any idea about why this happens?

最佳回答

为什么必须实施<代码>WithEvents和Handles。 净额 当你在VB中用<代码>WithEvents宣布一个田间时。 净额将作为财产产生。

每当通过转让作业更新该财产时,财产集造者将不向旧事件手提,然后以新价值为名承租活动手。 实际上,它喜欢这样做。

Property m_VarName as SomeType 
  Set   
    if _m_VarName isNot Nothing Then
      RemoveHandler _m_VarName.SomeEVent,m_VarName_OnCaptureStop
    End If
    _m_VarName = Value
    AddHandler _m_VarName.SomeEvent,m_VarName_OnCaptureStop
  End Set 
End Property

如果你认为,在你的法典中,主要情况如下:

ReleaseComObject(m_VarName)
RemoveHandler m_VarName.SomeEvent,m_VarName_OnCaptureStop

因此,请打电话<代码>RemoveHandler,打上已经释放的商用物体。 因此,它投下了<代码>Exception。

定本是简单的,不打上<代码>。 你实际需要使用这种方法的情况很少,几乎肯定不适用于这种情况。 相反,让理事会通常收集。

问题回答

暂无回答




相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签