Пример #1
0
        private void connectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            lstChatters.Items.Clear();
            NickDialog nickDlg = new NickDialog();

            if (nickDlg.ShowDialog() == DialogResult.OK)
            {
                myNick = nickDlg.txtNick.Text;
                nickDlg.Close();
            }

            txtMessage.Focus();
            Application.DoEvents();
            InstanceContext site = new InstanceContext(this);

            proxy = new ChatProxy(site);

            IAsyncResult iar = proxy.BeginJoin(myNick, new AsyncCallback((thisIar) =>
            {
                try
                {
                    string[] list = proxy.EndJoin(thisIar);

                    pwDlg.Invoke((Action)(() =>
                    {
                        if (list == null)
                        {
                            pwDlg.ShowError("Error: Username already exist!");
                            ExitChatSession();
                        }
                        else
                        {
                            pwDlg.Close();
                            ShowConnectMenuItem(false);
                            foreach (string name in list)
                            {
                                lstChatters.Items.Add(name);
                            }
                            AppendText("Connected at " + DateTime.Now.ToString() + " with user name " + myNick + Environment.NewLine);
                        }
                    }));
                }
                catch (Exception ex)
                {
                    pwDlg.Invoke((Action)(() =>
                    {
                        pwDlg.ShowError("Error: Cannot connect to chat!");
                        ExitChatSession();
                    }));
                }
            }), null);

            pwDlg = new PleaseWaitDialog();
            pwDlg.ShowDialog();
        }
Пример #2
0
 /// <summary>
 /// Is called as a callback from the asynchronous call, so simply get the
 /// list of <see cref="Common.Person">Chatters</see> that will
 /// be yielded as part of the Asynch Join call
 /// </summary>
 /// <param name="iar">The asnch result</param>
 private void OnEndJoin(IAsyncResult iar)
 {
     try
     {
         Person[] list = proxy.EndJoin(iar);
         HandleEndJoin(list);
     }
     catch (Exception e)
     {
         MessageBox.Show(e.Message, "Error",
                         MessageBoxButton.OK, MessageBoxImage.Error);
     }
 }