private void Login() { if (Global.Users != null && Global.Users.Count > 0) { LogMessage(null, "正在启动登录。"); List <KeyValuePair <string, SQLiteParameter[]> > sqlList = new List <KeyValuePair <string, SQLiteParameter[]> >(); foreach (var user in Global.Users) { bool isNeedUpdate = user.Id <= 0; AbstractPT pt = PTFactory.GetPT(user.Site.Id, user) as AbstractPT; pt.VerificationCode += Pt_VerificationCode; pt.TwoStepVerification += Pt_TwoStepVerification; string msg = pt.Login(); if (isNeedUpdate && pt.User != null && pt.User.Id > 0) { user.Id = pt.User.Id; string sql = "UPDATE USER SET USERID = @USERID WHERE PTSITEID = @PTSITEID AND USERNAME = @USERNAME "; SQLiteParameter[] parms = new SQLiteParameter[] { new SQLiteParameter("@USERID", DbType.Int32), new SQLiteParameter("@PTSITEID", DbType.Int32), new SQLiteParameter("@USERNAME", DbType.String), }; parms[0].Value = pt.User.Id; parms[1].Value = pt.User.Site.Id; parms[2].Value = pt.User.UserName; sqlList.Add(new KeyValuePair <string, SQLiteParameter[]>(sql, parms)); } LogMessage(user.Site, msg); } if (sqlList.Count > 0) { DBUtils.ExecuteNonQueryBatch(sqlList); } LogMessage(null, "全部登录完毕。"); } else { LogMessage(null, "请先添加用户。"); } }
private void SearchTorrent(List <PTSite> searchSites, string searchKey) { if (searchSites != null && searchSites.Count > 0) { bool isFill = false; List <PTTorrent> searchTorrents = new List <PTTorrent>(); StringBuilder sb = new StringBuilder(); var cts = new CancellationTokenSource(); YUEnums.PromotionType promotionType = (YUEnums.PromotionType)cmbPromotion.SelectedValue; YUEnums.AliveType aliveType = (YUEnums.AliveType)cmbAlive.SelectedValue; YUEnums.FavType favType = (YUEnums.FavType)cmbFav.SelectedValue; Task task = new Task(() => { Parallel.ForEach(searchSites.Distinct(), (site, state, i) => { //即便日后支持站点多用户,这里也只会取第一个用户。 AbstractPT pt = PTFactory.GetPT(site.Id, Global.Users.Where(x => x.Site.Id == site.Id).FirstOrDefault()) as AbstractPT; //假设4核Cpu,5个任务,因为是并行的原因,如果前4个在并行执行的过程任意一个发生了异常,那么此时前4个中其他3个还会继续执行到结束,但最后一个是不会执行的,所以这里需要做异常捕获处理。 try { List <PTTorrent> torrents = pt.SearchTorrent(searchKey, promotionType, aliveType, favType); lock (syncSearchObject) { if (torrents != null && torrents.Count > 0) { searchTorrents.AddRange(torrents); } } } catch (Exception ex) { lock (syncSearchObject) sb.AppendLine(ex.GetInnerExceptionMessage()); } }); }, cts.Token); progressPanel.BeginLoading(20000); task.Start(); cts.Token.Register(new Action(() => { if (!isFill) { FillTorrent(searchTorrents, ref isFill); bool isOpen = searchTorrents.Count <= 0; if (cts.Token.IsCancellationRequested) { LogMessage(null, "搜索过程出现错误,错误原因:超时。", isOpen); } } })); cts.CancelAfter(20000); task.ContinueWith(result => { if (!cts.Token.IsCancellationRequested && !isFill) { FillTorrent(searchTorrents, ref isFill); } string errMsg = sb.ToString(); if (!errMsg.IsNullOrEmptyOrWhiteSpace()) { bool isOpen = searchTorrents.Count <= 0; LogMessage(null, errMsg.TrimEnd(), isOpen); } }); } }