Click here to Skip to main content
15,885,985 members
Articles / Desktop Programming / XAML
Article

Communicating Between Two Local Silverlight Applications

Rate me:
Please Sign up or sign in to vote.
4.71/5 (3 votes)
5 Aug 2011CPOL1 min read 16.5K   14   1
A look at how to communicate between two local Silverlight Applications
Image 1

Introduction

One of the features that I’m surprised to see hardly no one talks about is “Communication Between Local Silverlight-Based Applications”. This is not new to Silverlight as it has been around since Silverlight 3. In other words, this will allow you to have two Silverlight applications running on the same PC talk to one another without using Web Services, etc. I originally was looking into this for a pet project that I was going to use with Kinect, but found this very valuable and decided to share with everyone.

Getting Started

We are going to create two separate Silverlight Applications (select Silverlight 4 or 5 Beta). The first application that we are going to create is the receiver.

Creating the Receiver

Launch Visual Studio 2010 and create a new Silverlight Application and name it “SilverlightReceiver”.

In your MainPage.xaml:

XML
<Grid x:Name="LayoutRoot" Background="Yellow">
    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" 
    TextWrapping="Wrap" Text="...Waiting for Message!" x:Name="txtReceiver"/>
</Grid>

In your MainPage.xaml.cs:

C#
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Messaging;
 
namespace SilverlightReceiver
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            Loaded += MainPage_Loaded;
        }
 
        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            var messageReceiver = new LocalMessageReceiver
            ("receiver", ReceiverNameScope.Global, LocalMessageReceiver.AnyDomain);
            messageReceiver.MessageReceived += receiver_MessageReceived;
            
            try
            {
                messageReceiver.Listen();
            }
            catch (ListenFailedException)
            {
                MessageBox.Show(
                    "Cannot receive messages." + Environment.NewLine +
                    "There is already a receiver with the name 'receiver'.",
                    "LocalMessageReceiver", MessageBoxButton.OK);
            }
        }
 
        private void receiver_MessageReceived(object sender, MessageReceivedEventArgs e)
        {
            txtReceiver.Text = e.Message;
            e.Response = "Message received";
        }
    }
}

Your MainPage.xaml for your Receiver should look like this in design mode:

Image 2

Creating the Sender

Launch Visual Studio 2010 and create a new Silverlight Application and name it “SilverlightSender”.

In your MainPage.xaml:

XML
<Grid x:Name="LayoutRoot">
    <Button Content="Send a Message" Click="SendClick" Margin="10"/>      
</Grid>

In your MainPage.xaml.cs:

C#
using System.Windows;
using System.Windows.Controls;
using System.Windows.Messaging;
 
namespace SilverlightSender
{
    public partial class MainPage : UserControl
    {
        private LocalMessageSender _messageSender = new LocalMessageSender("receiver");
 
        public MainPage()
          {
              InitializeComponent();
              Loaded += MainPage_Loaded;
          }
 
        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            _messageSender = new LocalMessageSender("receiver",LocalMessageSender.Global);
            _messageSender.SendCompleted += _sender_SendCompleted;
        }
  
          void _sender_SendCompleted(object sender, SendCompletedEventArgs e)
          {
              MessageBox.Show("Response \"" + e.Response + 
              "\" receieved.", "LocalMessageSender", MessageBoxButton.OK);
          }
  
          private void SendClick(object sender, RoutedEventArgs e)
        {
            _messageSender.SendAsync("Don't you love michaelcrump.net?");
         }
    }
}

Your MainPage.xaml for your Receiver should look like this in design mode:

Image 3

Time to run it!

Launch the receiver application first…

Image 4

Then the sender application second…

Image 5

Click the “Send a Message” button and you should instantly get:

Image 6

Now click on your receiver application…

Image 7

...and you will see it now has the message you sent from the “sender” application.

Conclusion

How cool is that? Plus it did not require any web services, etc! For future reference, you may want to check out these pages on MSDN (where I grabbed some of the code for this demo).

History

  • 3rd August, 2011: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Telerik
United States United States
Michael Crump is a Silverlight MVP and MCPD that has been involved with computers in one way or another for as long as he can remember, but started professionally in 2002. After spending years working as a systems administrator/tech support analyst, Michael branched out and started developing internal utilities that automated repetitive tasks and freed up full-time employees. From there, he was offered a job working at McKesson corporation and has been working with some form of .NET and VB/C# since 2003.

He has worked at Fortune 500 companies where he gained experience in embedded systems design and software development to systems administration and database programming, and everything in between.

His primary focus right now is developing healthcare software solutions using Microsoft .NET technologies. He prefers building infrastructure components, reusable shared libraries and helping companies define, develop and automate process standards and guidelines.

You can read his blog at: MichaelCrump.net or follow him on Twitter at @mbcrump.

Comments and Discussions

 
QuestionThere is ... Pin
Selvin5-Aug-11 4:01
Selvin5-Aug-11 4:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.