LocalMessageReceiver.Listen Method

Silverlight

Starts listening for messages from a LocalMessageSender.

Namespace:  System.Windows.Messaging
Assembly:  System.Windows (in System.Windows.dll)

'Declaration
<SecuritySafeCriticalAttribute> _
Public Sub Listen

ExceptionCondition
InvalidOperationException

The Listen method has already been called.

ObjectDisposedException

The Dispose method has already been called.

ListenFailedException

There is already a receiver registered with the same name and name scope.

This method registers the LocalMessageReceiver and enables it to receive messages.

After you call this method, the MessageReceived event will occur when a LocalMessageSender configured to send messages to this receiver calls its SendAsync method.

To stop receiving messages, you must call the Dispose method.

For more information, see Communication Between Local Silverlight-Based Applications.

The following code example demonstrates how to use this method. This example is part of a larger example available in How to: Implement Communication Between Local Silverlight-Based Applications.


Imports System
Imports System.Windows.Controls
Imports System.Windows.Messaging

Partial Public Class Receiver
    Inherits UserControl

    Public Sub New()

        InitializeComponent()

        Dim messageReceiver As New LocalMessageReceiver("receiver", _
            ReceiverNameScope.Global, LocalMessageReceiver.AnyDomain)
        AddHandler messageReceiver.MessageReceived, _
            AddressOf messageReceiver_MessageReceived

        Try

            messageReceiver.Listen()

        Catch ex As ListenFailedException

            output.Text = "Cannot receive messages." & Environment.NewLine & _
                "There is already a receiver with the name 'receiver'."

        End Try

    End Sub

    Private Sub messageReceiver_MessageReceived( _
        ByVal sender As Object, ByVal e As MessageReceivedEventArgs)

        e.Response = "response to " & e.Message
        output.Text = _
            "Message: " & e.Message & Environment.NewLine & _
            "NameScope: " & e.NameScope.ToString() & Environment.NewLine & _
            "ReceiverName: " & e.ReceiverName & Environment.NewLine & _
            "SenderDomain: " & e.SenderDomain & Environment.NewLine & _
            "Response: " & e.Response

    End Sub

End Class


Silverlight

Supported in: 5, 4, 3

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show: