Gavin Stevens's Blog

the ramblings of another developer....

  Home  |   Contact  |   Syndication    |   Login
  32 Posts | 0 Stories | 43 Comments | 215 Trackbacks

News

Archives

Maybe someone here can help me out with this little problem I've been having. I've been studying the remoting framework, mostly using Ingo Rammer's Advanced .NET Remoting book. In chapter 6, he presents a sample application on Enhanced Distributed Events.I've been trying to understand how this works and for the most part it all makes sense.
The disconnect comes when comparing the C# to VB.NET version of the sample code.

Here are the sample projects in both languages:

C# Sample Solution
VB.NET Sample Solution

A quick overview of the sample. The solution contains 4 projects: General - A Global Interface and class definition for a remotable object to be used to pass events Server - A server engine to load a singleton object to catch and relay events. EventListener - Project to create references to the server's singleton object to receive events. EventInitiator - Project to call server method to broadcast events.

Here is a piece of the Server code's implementation of the singleton class
Class Broadcaster
Inherits MarshalByRefObject
Implements IBroadcaster

Public Event MessageArrived As MessageArrivedHandler Implements IBroadcaster.MessageArrived

Public Sub BroadcastMessage(ByVal msg As String) Implements IBroadcaster.BroadcastMessage
Console.WriteLine("Will broadcast message: {0}", msg);
SafeInvokeEvent(msg);
End Sub

private void SafeInvokeEvent(String msg) {
// call the delegates manually to remove them if they aren't
// active anymore.

if (MessageArrived == null)
{Console.WriteLine("No listeners");}
else
{ //Console.WriteLine("Number of Listeners: {0}",MessageArrived.GetInvocationList().Length);
MessageArrivedHandler mah=null;

foreach (Delegate del in MessageArrived.GetInvocationList()) {
try {
mah = (MessageArrivedHandler) del;
mah(msg);
} catch (Exception e) {
Console.WriteLine("Exception occured, will remove Delegate");
MessageArrived -= mah;
}
}
}
}
You can see in the line "if (MessageArrived == null)" this is referencing the Public Event MessageArrived As MessageArrivedHandler Implements IBroadcaster.MessageArrived, then on the VB Implementation:
Class Broadcaster
Inherits MarshalByRefObject
Implements IBroadcaster

Public Event MessageArrived As MessageArrivedHandler Implements IBroadcaster.MessageArrived

Public Sub BroadcastMessage(ByVal msg As String) Implements IBroadcaster.BroadcastMessage
Console.WriteLine("Will broadcast message: {0}", msg)
SafeInvokeEvent(msg)
End Sub

Private Sub SafeInvokeEvent(ByVal msg As String)
' call the delegates manually and remove them if they aren't
' active anymore
Dim mah As MessageArrivedHandler
Dim del As System.Delegate

If MessageArrivedEvent Is Nothing Then
Console.WriteLine("No Listeners")
Else
For Each del In MessageArrivedEvent.GetInvocationList()
Try
mah = CType(del, MessageArrivedHandler)
' call a single client taken from the list
mah(msg)
Catch e As Exception
Console.WriteLine("Exception occurred, will remove Delegate")
MessageArrivedEvent = CType(System.Delegate.Remove(MessageArrivedEvent, del), MessageArrivedHandler)
End Try
Next
End If
End Sub

In this implementation the same code as in the C# version
If MessageArrivedEvent Is Nothing Then
is now referencing
Public Event MessageArrived As MessageArrivedHandler Implements IBroadcaster.MessageArrived

How does this make sense? Where is the MessageArrivedEvent declared? Does VB somehow rename the multicast delagate? It makes sense in C# where MessageArrived is declared in the code, but the VB.NET version seems to change the name somehow. I would appreciate your input on this one. Maybe someone has seen a really good article on VB.NET and Multicast delegates, or can help me out with this obvious discrepency.


Thanks,
Gavin S
posted on Monday, August 02, 2004 5:00 PM

Feedback

# re: Remoting, MultiCast Delagates Problem.... HELP!!! 11/8/2004 7:31 PM Eric
Gavin, I don't know if you've discovered this yet, but the code actually does make sense. VB.Net's compiler automagically creates the MessageArrivedEvent and MessageArrivedHandler types/variables when you declare the event MessageArrived. These types are "hidden" from Intellisense, but the code actually compiles and works. (I am not sure whether they become visible if you uncheck "Hide Advanced Members" in the VB.Net options dialog.)

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: