Aracılığıyla paylaş


LocalMessageSender.SendCompleted Event

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Occurs when the message has been successfully sent.

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

Syntax

'Declaration
Public Event SendCompleted As EventHandler(Of SendCompletedEventArgs)
public event EventHandler<SendCompletedEventArgs> SendCompleted

Remarks

You can handle this event to determine whether a message was sent successfully and to retrieve any response message. If the message was not received, the AsyncCompletedEventArgs.Error property (inherited by the SendCompletedEventArgs class) will be set to a SendFailedException instance. This can occur, for example, if the specified receiver name has not been registered or if the receiver is not configured to receive messages from the sender's domain.

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

Examples

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

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

Partial Public Class Sender
    Inherits UserControl

    Private messageSender As LocalMessageSender

    Public Sub New()

        InitializeComponent()
        UpdateButton()
        messageSender = New LocalMessageSender( _
            "receiver", LocalMessageSender.Global)
        AddHandler messageSender.SendCompleted, _
            AddressOf sender_SendCompleted
        SendMessage("message from Sender constructor")

    End Sub

    Private clickNumber As Integer = 1

    Private Sub UpdateButton()
        button.Content = "send message 'click " & clickNumber & "'"
    End Sub

    Private Sub Button_Click(ByVal sender As Object, _
        ByVal e As RoutedEventArgs)

        SendMessage("click " & clickNumber)
        clickNumber += 1
        UpdateButton()

    End Sub

    Private Const MAX_ATTEMPTS As Integer = 10000
    Private attempt As Integer = 1

    Private Sub SendMessage(ByVal message As String)
        messageSender.SendAsync(message, attempt)
    End Sub

    Private Sub sender_SendCompleted(ByVal sender As Object, _
        ByVal e As SendCompletedEventArgs)

        If e.Error IsNot Nothing Then

            LogError(e)
            attempt += 1
            If attempt > MAX_ATTEMPTS Then
                output.Text = "Could not send message."
                Return
            End If
            SendMessage(e.Message)
            Return

        End If

        output.Text = _
            "Message: " & e.Message & Environment.NewLine & _
            "Attempt " & CType(e.UserState, Integer) & _
            " completed." & Environment.NewLine & _
            "Response: " & e.Response & Environment.NewLine & _
            "ReceiverName: " & e.ReceiverName & Environment.NewLine & _
            "ReceiverDomain: " & e.ReceiverDomain

        ' Reset attempt counter.
        attempt = 1

    End Sub

    Private Sub LogError(ByVal e As SendCompletedEventArgs)
        System.Diagnostics.Debug.WriteLine( _
            "Attempt number {0}: {1}: {2}", CType(e.UserState, Integer), _
            e.Error.GetType().ToString(), e.Error.Message)
    End Sub

End Class
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);
        }

    }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

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