web方式获取MSN好友列表,解决线程问题

 DotMSN已经可以用来取MSN用户列表了,可是登录回调事件在web上很难做,用异步是可以记录到数据库,可是不方便马上返回给客户端。这个问题一直没有很好的解决(有用sleep的,可是这样不能保证可以得到返回,当然也有可能会浪费时间),下面的做法解决了这个问题。
看来自己对多线程要好好的深入研究一下了。

///构造好友姓名与邮箱

 public class FriendInfo
    {
        private string username;
        private string email;
        public FriendInfo(string username, string email)
        {
            this.username = username;
            this.email = email;
        }
        public string UserName
        {
            get { return username; }
            set { username = value; }
        }
        public string Email
        {
            get { return email; }
            set { email = value; }
        }
      
    }

 

 

public class GetFriendInfo
    {
        protected   XihSolutions.DotMSN.Messenger     messenger   =   new Messenger();
        protected string xmlPath = "";
        protected AutoResetEvent are;//多线程同步信号
        IList<FriendInfo> models = new List<FriendInfo>();
        public void Connect()
        {

            try
            {
                messenger.Credentials.ClientID = "msmsgs@msnmsgr.com";
                messenger.Credentials.ClientCode = "Q1P7W2E4J9R8U3S5";
                messenger.Credentials.Account = "yours@hotmail.com";
                messenger.Credentials.Password = "password";
                if(messenger.Connected)
                    messenger.Disconnect();
                messenger.Connect();
                are.WaitOne(); //UI线程等待
            }
            catch
            {

            }

        }
        /// <summary>
        /// 获取列表开始
        /// </summary>
        public void GetFriendList()
        {
            are = new AutoResetEvent(false);
//            messenger.Nameserver.SignedIn += new EventHandler(Nameserver_SignedIn);
            messenger.Nameserver.SignedIn += Nameserver_SignedIn;
            Connect();
          
        }
        private void Nameserver_SignedIn(object sender, EventArgs e)
        {

            try
            {
                messenger.Owner.Status = PresenceStatus.Online;
                SetFriendInfo();
                //UpdateContactlist();
              
            }
            catch
            {

            }


        }
       
        /// <summary>
        /// 填充好友列表
        /// </summary>
        private void SetFriendInfo()
        {
            try
            {
                if(messenger.Connected == false)
                    return;
                FriendInfo model = null;
                foreach(Contact contact in messenger.ContactList.Allowed)
                {
                   model = new FriendInfo(contact.Name,contact.Mail);
                   models.Add(model);
                }
                are.Set();//通知UI线程可以继续干活了。。
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 返回列表数据
        /// </summary>
        /// <returns></returns>
        public IList<FriendInfo> GetFriendListInfo()
        {
            return models;
        }

   }

///以下是aspx页面调用

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetFriendInfo getFriendInfo = new GetFriendInfo();
                getFriendInfo.GetFriendList();
                IList<FriendInfo >model = getFriendInfo.GetFriendListInfo();
                foreach(FriendInfo info in model)
                {
                    Response.Write(info.UserName+"||"+info.Email + "<br/>");
                }
             }
         
        }

posted @ 2009-08-01 12:37  no name is my name  阅读(982)  评论(4编辑  收藏  举报