![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
|
C# Begin using System; |
#2
| |||
| |||
|
|
Hello, I have a class in C# with events, which is exposed as a COM-object. When I have a VB6 Client that is using this class, I see that eventhandlers are not removed, when VB releases the com object. I'm using VS2005, but it seems that the behaviour is the same with VS2003 Is there something wrong with the way I'm using this, or may it be that this is an error with NET or VB? It doesn't seem to be related to the garbage collector, because when I call GC.Collect(), remove doesn't get called either. Thanks in advance Sascha Here is my code C#: Register for COM interop has to be enabled C# Begin using System; using System.Runtime.InteropServices; using System.Diagnostics; namespace NetComLib { // delegate definition [ComVisible(false)] public delegate void ChannelChangedDelegate(int channel); // Event-Interface [ComVisible(true)] [Guid("30BBBC2F-03E0-497c-82BF-93B1D775B794")] [InterfaceType(ComInterfaceType.InterfaceIsIDispatc h)] public interface INetComLibEvents { void ChannelChanged(int channel); } // Interface [ComVisible(true)] [Guid("0EB15C71-DF7B-46e3-8686-2A7708D9487A")] [InterfaceType(ComInterfaceType.InterfaceIsIUnknown )] public interface INetComLib { void SetChannel(int channelNo); } // COM-object [ComVisible(true)] [ProgId("Test.NetComLib")] [Guid("C2D2CE9B-09A6-475e-A839-C8F90C630F43")] [ClassInterface(ClassInterfaceType.None)] [ComSourceInterfaces("NetComLib.INetComLibEvents")] public class NetComLib : INetComLib { public void SetChannel(int channelNo) { if (channelChanged != null) channelChanged(channelNo); } private event ChannelChangedDelegate channelChanged; public event ChannelChangedDelegate ChannelChanged { [System.Runtime.CompilerServices.MethodImpl(System. Runtime.CompilerServices.MethodImplOptions.Synchro nized)] add { this.channelChanged += value; Trace.WriteLine("Eventhandler added"); } [System.Runtime.CompilerServices.MethodImpl(System. Runtime.CompilerServices.MethodImplOptions.Synchro nized)] remove { this.channelChanged -= value; Trace.WriteLine("Eventhandler removed"); } } } } C# End And here is the code of my VB-Form with one button VB Begin Dim WithEvents comLib As NetComLib.NetComLib Dim i As Integer Private Sub comLib_ChannelChanged(ByVal channel As Long) MsgBox channel End Sub Private Sub Command1_Click() If comLib Is Nothing Then Set comLib = New NetComLib.NetComLib comLib.SetChannel i i = i + 1 Else Set comLib = Nothing End If End Sub VB End |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |