Chaining WCF Async Methods in Silverlight

I'm developing a silverlight application and consuming WCF service with Async methods. But I couldnt make it working.

In my page I have the code below. Basically I want to first call LoginAsync then GetTablesAsync then GetPlayersAsync and meanwhile I need to call the client notify methods as well. How can I arrange it ? With Locks or is there a method for chaining these events to make sure that they will do fine?

public partial class Root : UserControl
{
    private object syncRoot = new object();
    ServiceClient client;

    ObservableCollection<Table> Tables { get; set; }
    ObservableCollection<Player> Players { get; set; }
    public Root()
    {
        InitializeComponent();

        Tables = new ObservableCollection<Table>();

        txtLogs.Text += "\n";

        client = Helpers.GetServiceClient();

        client.NotifyReceived += new EventHandler<NotifyReceivedEventArgs>(client_NotifyReceived);

        client.PublishCompleted += new EventHandler<AsyncCompletedEventArgs>(client_PublishCompleted);
        client.SubscribeCompleted += new EventHandler<AsyncCompletedEventArgs>(client_SubscribeCompleted);
        client.GetTableListCompleted += client_GetPokerTablesCompleted;



        this.Loaded += Root_Loaded;
    }

    void Root_Loaded(object sender, RoutedEventArgs e)
    {
        NewUser newUser = new NewUser();
        newUser.Closed += newUser_Closed;
        newUser.Show();
    }

    void newUser_Closed(object sender, EventArgs e)
    {
        NewUser n = sender as NewUser;
        player = new Player
        {
            PlayerName = n.txtPlayerName.Text,
        };

        lock (syncRoot)
        {

            try
            {
                client.GetTableListAsync();
            }
            catch (TimeoutException ex)
            {

            }
            catch (CommunicationException ex)
            {

            }
        }


        lock (syncRoot)
        {
            client.LoginCompleted += client_LoginCompleted;
            client.LoginAsync(player.PlayerName);
            // publish player logged in
            client.PublishAsync(String.Format("{0} is logged in", player.PlayerName));
        }


    }

Answers


I have found AsyncCTP most useful for the scenario you described. However, you can achieve roughly the same readability as roughly javascript libraries if you use the following pattern:

NewUser newUser = new NewUser();
newUser.Show();
newUser.Closed += (object sender, EventArgs e) =>
{
    player = new Player
    {
        PlayerName = newUser.txtPlayerName.Text,
    };

    // you aren't going to be able to catch timeout
    // errors in async method calls
    client.GetTableListAsync();

    // we can initiate login even if TableList isn't loaded yet
    client.LoginAsync(player.PlayerName);
    client.LoginCompleted += (s2, e2) => {
        // publish player logged in
        client.PublishAsync(String.Format("{0} is logged in", player.PlayerName));
    }
}

Do you mean you want to call them one after another? You could call the second one from the Login completed handler (then the third from the handler for the second one completing etc).


Need Your Help

Showing percentages above bars on Excel column graph

excel charts excel-formula

I have a graph that is pulling data from a PivotTable, so I get a distribution of counts (how many in category A, how many in B, how many in C, etc)

How to find the first unique encounters of a sub string in R

r select substring unique

Could you please help me with the following R scripting problem:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.