2009-05-12 5 views
3

VB.NET 구성 요소에서 발생하는 이벤트를 처리하는 클래스를 인스턴스화하는 일부 VB6 코드가 있습니다. VB6은 매우 간단합니다 : 이벤트 핸들러 객체가 init 메소드의 범위를 벗어 살고있다VB.NET 코드에서 VB.NET 이벤트 처리

private m_eventHandler as new Collection 

... 

public sub InitSomething() 
    dim handler as EventHandler 

    set handler = new EventHandler 
    m_eventHandler.Add handler 
    ... 
    m_engine.Start 

end sub 

하는 것으로 (이 컬렉션에 저장되는 이유입니다). m_engine.Start은 VB.NET 구성 요소가 이벤트를 발생시키는 프로그램의 지점을 나타냅니다. (요구 된 바와 같이)

실제 이벤트 핸들러 :

Private WithEvents m_SomeClass As SomeClass 
Private m_object as Object 
... 

Private Sub m_SomeClass_SomeEvent(obj As Variant) 
    Set obj = m_object 
End Sub 

참고 EventHandler의 인스턴스가 생성 될 때 m_object 초기화된다.

이벤트를 발생시키는 VB.NET 코드도 간단하다 :

Public ReadOnly Property SomeProp() As Object 
    Get 
     Dim obj As Object 
     obj = Nothing 
     RaiseEvent SomeEvent(obj) 
     SomeProp = obj 
    End Get 
End Property 

내 문제를 내가 디버그 VB6 프로그램, 처음 InitSomething가 호출되는 경우, 이벤트 것이다 하지 (VB6 이벤트 핸들러는 입력되지 않습니다.) 이후에 InitSomething에 대한 호출이 가능합니다.

디버거 외부에서 프로그램을 실행할 때 예상했던대로 작동합니다. 이 시점에서, 나는 이것이 내가 걱정해야만하는 것인지 확실하지 않다.

VB.NET이 비주얼 스튜디오 코드 변환 도구를 사용하여 VB6에서 변환되었지만 이후에는 수동으로 정리되었습니다.

+0

실제 이벤트 핸들러와 핸들링 오브젝트의 선언을 표시 할 수 있습니까? ("WithEvents"가있는 것)? –

답변

0

그냥 뭔가 시도 - 나는의 고유의 불신이 "새로를 .."

당신이

private m_eventHandler as Collection 

public sub InitSomething() 
    dim handler as EventHandler 

    set handler = new EventHandler 

    If m_eventHandler Is Nothing Then 
    Set m_eventHandler = New Collection 
    End if 

    m_eventHandler.Add handler 
... 

    m_engine.Start 

end sub 

아아를 시도 할 수 있습니다,이 정상적인 실행이 작동 왜 아무 생각이 없어 및했습니다 디버그에는 .NET에서 VBA.Collection 개체 (VB 구성 요소를 작성하는 것이 좋습니다)를 인스턴스화 할 수 없다는 모호한 의심을 제외하면 .NET 코드에서 컬렉션을 만들지 않으므로 , 그것은 여전히 ​​막연한 의혹입니다.

5

VB6 (또는 다른 COM 환경)에서 .Net Components for Consumption을 쓰고 있다면 인터페이스 사용이 절대적으로 비판적이라는 것을 알게되었습니다.

VStudio와 함께 제공되는 COM 템플릿은 특히 이벤트를 작동 시키려고 할 때 많이 필요합니다.

다음은 내가 사용한 것입니다.

Imports System.Runtime.InteropServices 
Imports System.ComponentModel 

<InterfaceType(ComInterfaceType.InterfaceIsDual), Guid(ClientAction.InterfaceId)> Public Interface IClientAction 
     <DispId(1), Description("Make the system raise the event")> sub SendMessage(ByVal theMessage As String) 
    End Interface 


    <InterfaceType(ComInterfaceType.InterfaceIsIDispatch), Guid(ClientAction.EventsId)> Public Interface IClientActionEvents 
     <DispId(1)> Sub TestEvent(ByVal sender As Object, ByVal e As PacketArrivedEventArgs) 
    End Interface 



    <ComSourceInterfaces(GetType(IClientActionEvents)), Guid(ClientAction.ClassId), ClassInterface(ClassInterfaceType.None)> _ 
    Public Class ClientAction 
     Implements IClientAction 

     Public Delegate Sub TestEventDelegate(ByVal sender As Object, ByVal e As PacketArrivedEventArgs) 

     Public Event TestEvent As TestEventDelegate 

    public sub New() 
     //Init etc 
    end sub 

    public sub SendMessage(theMessage as string) implements IClientAction.SendMessage 
     onSendMessage(theMessage) 
    end sub 

     Protected Sub onSendMessage(message as string) 
      If mRaiseEvents Then 
       RaiseEvent TestEvent(Me, New PacketArrivedEventArgs(theMessage)) 
      End If 
     End Sub 

    end Class 

나는 조립/구성 요소의 COM 및 .NET 소비자가 이벤트 제대로 작동하고 및 구성 요소에서 디버깅 할 수 있도록 얻을 수있었습니다.

희망이 도움이됩니다.