Click here to Skip to main content
15,896,201 members
Articles / Programming Languages / C#

A Simple Solution to Some Problems with Asynchrony in Silverlight

Rate me:
Please Sign up or sign in to vote.
4.69/5 (8 votes)
9 Mar 2010CPOL15 min read 24.9K   128   25  
A small, extensible suite of classes that elegantly solve some common problems involving asynchrony and event handling that tend to occur when Silverlight and WCF are used together.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading;
using TaskManagerDemo.Web.DataContracts;

namespace TaskManagerDemo.Web
{
    // NOTE: If you change the class name "ExampleService" here, you must also update the reference to "ExampleService" in Web.config.
    public class ExampleService : IExampleService
    {
        public List<CityZipCode> GetSelectedCities(int factor)
        {
            // Sleep to simulate a database query that takes some time.
            Thread.Sleep(500);
            return GetRandomSelection(factor);
        }

        public List<CityZipCode> GetSelectedZipCodes(int factor)
        {
            Thread.Sleep(500);
            return GetRandomSelection(factor);
        }

        private List<CityZipCode> GetRandomSelection(int factor)
        {
            List<CityZipCode> result = GetAllCityZipCodes();
            Random random = new Random();
            int maxValue = (factor % 7) + 3;
            result = result.Where(czc => random.Next(0, maxValue) == 0).ToList();
            return result;
        }

        public List<CityZipCode> GetAllCityZipCodes()
        {
            Thread.Sleep(500);
            return new List<CityZipCode>
            {
                new CityZipCode { City = "Saginaw", ZipCode = "98541" },
                new CityZipCode { City = "Saint John", ZipCode = "99127" },
                new CityZipCode { City = "Saint John", ZipCode = "99171" },
                new CityZipCode { City = "Saint Urbans", ZipCode = "98596" },
                new CityZipCode { City = "Salkum", ZipCode = "98582" },
                new CityZipCode { City = "Samish Island", ZipCode = "98232" },
                new CityZipCode { City = "Sammamish", ZipCode = "98074" },
                new CityZipCode { City = "Sammamish", ZipCode = "98075" },
                new CityZipCode { City = "San Juan Island", ZipCode = "98250" },
                new CityZipCode { City = "Sandy Hook Park", ZipCode = "98370" },
                new CityZipCode { City = "Sappho", ZipCode = "98305" },
                new CityZipCode { City = "Satsop", ZipCode = "98951" },
                new CityZipCode { City = "Sawyer", ZipCode = "98951" },
                new CityZipCode { City = "Saxon", ZipCode = "98220" },
                new CityZipCode { City = "Scandia", ZipCode = "98370" },
                new CityZipCode { City = "Scenic", ZipCode = "98288" },
                new CityZipCode { City = "Schneiders Prairie", ZipCode = "98501" },
                new CityZipCode { City = "Seabeck", ZipCode = "98380" },
                new CityZipCode { City = "Seahurst", ZipCode = "98062" },
                new CityZipCode { City = "Seatac", ZipCode = "98148" },
                new CityZipCode { City = "Seatac", ZipCode = "98158" },
                new CityZipCode { City = "Seatac", ZipCode = "98168" },
                new CityZipCode { City = "Seatac", ZipCode = "98188" },
                new CityZipCode { City = "Seatac", ZipCode = "98198" },
                new CityZipCode { City = "Seattle", ZipCode = "98101" },
                new CityZipCode { City = "Seattle", ZipCode = "98102" },
                new CityZipCode { City = "Seattle", ZipCode = "98103" },
                new CityZipCode { City = "Seattle", ZipCode = "98104" },
            };
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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) Concur
United States United States
George Henry has worked as a software developer for more than 20 years. He is currently employed by Concur in Bellevue, Washington, USA.

Comments and Discussions