Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

LocalMessageSender.SendAsync Method (String, Object)

Silverlight

Sends the specified messages to the configured receiver asynchronously.

Namespace:  System.Windows.Messaging
Assembly:  System.Windows (in System.Windows.dll)
[SecuritySafeCriticalAttribute]
public void SendAsync(
	string message,
	Object userState
)

Parameters

message
Type: System.String
The message to send.
userState
Type: System.Object
A unique user-state object that functions as a task ID for the message transfer.
ExceptionCondition
ArgumentNullException

message is null.

ArgumentException

message is longer than 40,960 characters.

You can send messages up to 40 kilobytes long.

When you call this method, the MessageReceived event occurs for the LocalMessageReceiver that corresponds to the ReceiverName and ReceiverDomain settings. Then, the SendCompleted event occurs, regardless of whether the message was successfully sent. You can retrieve the userState object in the SendCompleted event handler in order to identify the message.

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.


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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.