Click here to Skip to main content
Click here to Skip to main content

Communicating Between Two Local Silverlight Applications

, 5 Aug 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
A look at how to communicate between two local Silverlight Applications

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:

<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:

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:

Creating the Sender

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

In your MainPage.xaml:

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

In your MainPage.xaml.cs:

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:

Time to run it!

Launch the receiver application first…

Then the sender application second…

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

Now click on your receiver application…

...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)

Share

About the Author

mbcrump
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.
Follow on   Twitter

Comments and Discussions

 
QuestionThere is ... PinmemberSelvin5-Aug-11 5:01 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150414.1 | Last Updated 5 Aug 2011
Article Copyright 2011 by mbcrump
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid