Export (0) Print
Expand All

LocalMessageSender Class

Silverlight

Represents the sending end of a local messaging channel between two Silverlight-based applications.

System.Object
  System.Windows.Messaging.LocalMessageSender

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

public sealed class LocalMessageSender

The LocalMessageSender type exposes the following members.

  NameDescription
Public methodLocalMessageSender(String)Initializes a new instance of the LocalMessageSender class and configures it to send messages to the receiver with the specified name.
Public methodLocalMessageSender(String, String)Initializes a new instance of the LocalMessageSender class and configures it to send messages to the receiver with the specified name and domain.
Top

  NameDescription
Public propertyReceiverDomainGets the domain of the LocalMessageReceiver that this sender will send messages to.
Public propertyReceiverNameGets the name of the LocalMessageReceiver that this sender will send messages to.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSendAsync(String)Sends the specified message to the configured receiver asynchronously.
Public methodSendAsync(String, Object)Sends the specified messages to the configured receiver asynchronously.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Public eventSendCompletedOccurs when the message has been successfully sent.
Top

  NameDescription
Public fieldStatic memberGlobalA value that represents the global domain.
Top

A LocalMessageSender object can send messages to a LocalMessageReceiver object in a different Silverlight-based application running on the same computer. For information about using these classes, see Communication Between Local Silverlight-Based Applications.

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


using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Messaging;

namespace SendingApplication
{
    public partial class Sender : UserControl
    {
        private LocalMessageSender messageSender;

        public Sender()
        {
            InitializeComponent();
            UpdateButton();
            messageSender = new LocalMessageSender(
                "receiver", LocalMessageSender.Global);
            messageSender.SendCompleted += sender_SendCompleted;
            SendMessage("message from Sender constructor");
        }

        private int clickNumber = 1;

        private void UpdateButton()
        {
            button.Content = "send message 'click " + clickNumber + "'";
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            SendMessage("click " + clickNumber);
            clickNumber++;
            UpdateButton();
        }

        private const int MAX_ATTEMPTS = 10000;
        private int attempt = 1;

        private void SendMessage(string message)
        {
            messageSender.SendAsync(message, attempt);
        }

        private void sender_SendCompleted(object sender, SendCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                LogError(e);
                attempt++;
                if (attempt > MAX_ATTEMPTS)
                {
                    output.Text = "Could not send message.";
                    return;
                }
                SendMessage(e.Message);
                return;
            }

            output.Text =
                "Message: " + e.Message + Environment.NewLine +
                "Attempt " + (int)e.UserState + 
                " completed." + Environment.NewLine +
                "Response: " + e.Response + Environment.NewLine +
                "ReceiverName: " + e.ReceiverName + Environment.NewLine + 
                "ReceiverDomain: " + e.ReceiverDomain;

            // Reset attempt counter.
            attempt = 1;
        }

        private void LogError(SendCompletedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine(
                "Attempt number {0}: {1}: {2}", (int)e.UserState, 
                e.Error.GetType().ToString(), e.Error.Message);
        }

    }
}


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.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

ADD
Show:
© 2014 Microsoft