public void DoLogic() { sCB.IsEnabled = false; smCB.IsEnabled = false; overTimeTB.Text = "授权时间截止至 [ " + Core.Constant.overtime + " ]"; //dLV.IsEnabled = false; uin = Core.Constant.Uin; List <Object> _contact_all = new List <object>(); ((Action)(delegate() { LoginCore.InitCookie(uin); string sid = LoginCore.GetPassTicket(uin).WxSid; string host = LoginCore.GetPassTicket(uin).WxHost; WXService wxs = new WXService { Uin = uin, Sid = sid, DeviceID = "e" + LoginCore.GenerateCheckCode(15), BaseUrl = "https://" + LoginCore.GetPassTicket(uin).WxHost, PushUrl = "https://webpush." + LoginCore.GetPassTicket(uin).WxHost, UploadUrl = "https://file." + LoginCore.GetPassTicket(uin).WxHost }; JObject init_result = wxs.WxInit(); //初始化 var partUsers = new List <WXUser>(); if (init_result != null) { this.Dispatcher.BeginInvoke(((Action) delegate() //等待结束 { supLB.Content = "加载个人信息..."; })); var _me = new WXUser { uin = wxs.Uin, UserName = init_result["User"]["UserName"].ToString(), City = "", HeadImgUrl = init_result["User"]["HeadImgUrl"].ToString(), NickName = init_result["User"]["NickName"].ToString(), Province = "", PYQuanPin = init_result["User"]["PYQuanPin"].ToString(), RemarkName = init_result["User"]["RemarkName"].ToString(), RemarkPYQuanPin = init_result["User"]["RemarkPYQuanPin"].ToString(), Sex = init_result["User"]["Sex"].ToString(), Signature = init_result["User"]["Signature"].ToString(), }; forwardUser = _me.UserName; partUsers.Add(_me); this.Dispatcher.BeginInvoke((Action)(delegate() //等待结束 { //var aa = wxs.BaseUrl + _me.HeadImgUrl; //headImage.Source = new BitmapImage(new Uri(aa)); headImage.Source = BitmapFrame.Create(wxs.GetIcon(_me.UserName, uin), BitmapCreateOptions.None, BitmapCacheOption.Default); })); this.Dispatcher.BeginInvoke(((Action) delegate() //等待结束 { supLB.Content = "加载最近联系人..."; })); foreach (JObject contact in init_result["ContactList"]) //部分好友名单 { WXUser user = new WXUser(); user.uin = wxs.Uin; user.UserName = contact["UserName"].ToString(); user.City = contact["City"].ToString(); user.HeadImgUrl = contact["HeadImgUrl"].ToString(); user.NickName = contact["NickName"].ToString(); user.Province = contact["Province"].ToString(); user.PYQuanPin = contact["PYQuanPin"].ToString(); user.RemarkName = contact["RemarkName"].ToString(); user.RemarkPYQuanPin = contact["RemarkPYQuanPin"].ToString(); user.Sex = contact["Sex"].ToString(); user.Signature = contact["Signature"].ToString(); partUsers.Add(user); } var _syncKey = new Dictionary <string, string>(); foreach (JObject synckey in init_result["SyncKey"]["List"]) //同步键值 { _syncKey.Add(synckey["Key"].ToString(), synckey["Val"].ToString()); } //保存最新key LoginCore.SyncKey(uin, _syncKey); this.Dispatcher.BeginInvoke(((Action) delegate() //等待结束 { supLB.Content = "初始化联系人..."; })); WxContact _contact = new WxContact(uin); //记住此处不适合再开线程 _contact.InitContact(partUsers); //初始联系人 Dictionary <string, string> Groups = new Dictionary <string, string>(); foreach (var g in _contact.GetGroupUserNames()) { Groups.Add(g, _contact.GetGroupMemberNames(g).NickName); } this.Dispatcher.BeginInvoke((Action)(delegate() //等待结束 { sCB.ItemsSource = Groups; sCB.DisplayMemberPath = "Value"; sCB.SelectedValuePath = "Key"; smCB.DisplayMemberPath = "Value"; smCB.SelectedValuePath = "Key"; supLB.Content = "进入监听模式..."; sCB.IsEnabled = true; smCB.IsEnabled = true; //dLV.IsEnabled = true; })); string sync_flag = null; JObject sync_result; sync_flag = wxs.WxSyncCheck(); //同步检查 DateTime lastCheckTs = DateTime.Now; while (true) { if (sync_flag != null) { var retcode = sync_flag.ToString().Split(new string[] { "\"" }, StringSplitOptions.None)[1]; var selector = sync_flag.ToString().Split(new string[] { "\"" }, StringSplitOptions.None)[3]; //this.Dispatcher.BeginInvoke((Action)(delegate () //{ //supLB.Content = "{retcode:" + retcode + " selector:" + selector + "}"; //})); if (retcode == "1100") { this.Dispatcher.BeginInvoke((Action)(delegate() { headBorder.BorderBrush = Brushes.Red; })); MessageBox.Show("你在手机上登出了微信,债见"); break; } if (retcode == "1101") { this.Dispatcher.BeginInvoke((Action)(delegate() { headBorder.BorderBrush = Brushes.Red; })); MessageBox.Show("你在其他地方登录了 WEB 版微信,债见"); break; } else if (retcode == "0") { this.Dispatcher.BeginInvoke((Action)(delegate() { headBorder.BorderBrush = Brushes.GreenYellow; })); if (selector == "2") { sync_result = wxs.WxSync(); //进行同步 lastCheckTs = DateTime.Now; if (sync_result != null) { handleMsg(sync_result); } //this.Dispatcher.BeginInvoke((Action)(delegate () //{ //supLB.Content = ""; //})); } else if (selector == "0") { //this.Dispatcher.BeginInvoke((Action)(delegate () //{ //supLB.Content = ""; //})); Thread.Sleep(500); } else { sync_result = wxs.WxSync(); //进行同步 this.Dispatcher.BeginInvoke((Action)(delegate() { supLB.Content = "最后同步:{retcode:" + retcode + " selector:" + selector + "}"; })); //this.Dispatcher.BeginInvoke((Action)(delegate () //{ //chatText.AppendText("\n" + selector + "\n-------" + sync_result.ToString() + "-------\n"); //})); } } sync_flag = null; this.Dispatcher.BeginInvoke((Action)(delegate() { headBorder.BorderBrush = Brushes.Blue; })); } if ((DateTime.Now - lastCheckTs).Seconds < 23) { var sleep = (DateTime.Now - lastCheckTs).Seconds; Thread.Sleep(1000); sync_flag = wxs.WxSyncCheck(); //同步检查 } else { sync_flag = wxs.WxSyncCheck(); lastCheckTs = DateTime.Now; } this.Dispatcher.BeginInvoke((Action)(delegate() { sleepTB.Text = ((DateTime.Now - lastCheckTs).Seconds).ToString(); supLB.Content = "最后同步:" + DateTime.Now.ToLongTimeString().ToString(); })); } } })).BeginInvoke(null, null); }