private void fc_FormClosed(object sender, FormClosedEventArgs e) { frmIconCheck fc = (frmIconCheck)sender; if (!fc.CheckOver) { this.CrossThreadDo(() => { this.txtShow.AppendText("取消验证码输入,停止所有下载线程,等待中..."); this.txtShow.AppendText("\r\n"); }); } else { downContinual = true; th = new System.Threading.Thread[threadCount]; for (int i = 0; i < threadCount; i++) { int index = i; th[i] = new System.Threading.Thread(() => DownLoad(allUrl, index, txtFile.Text)); th[i].IsBackground = true; th[i].Start(); } } fc.FormClosed -= fc_FormClosed; fc.Dispose(); }
private void CheckTooMany(IconValue icon, int index, string dir) { lock (lockObject) { string html = GetHtmlString(icon.Url); if (html.IndexOf("TODAY, Too many download requests.") >= 0) { downContinual = false; this.CrossThreadDo(() => { fc = new frmIconCheck(); fc.Url = icon.Url; fc.Dir = dir; fc.FormClosed += fc_FormClosed; for (int i = 0; i < threadCount; i++) { if (i != index) { switch (th[i].ThreadState) { case System.Threading.ThreadState.WaitSleepJoin | System.Threading.ThreadState.Background: //线程LOCK时,直接停止 th[i].Abort(); break; case System.Threading.ThreadState.Running: //正在下载时,等待此次下载完成 th[i].Join(5000); break; default: this.txtShow.AppendText("错误的状态" + th[i].ThreadState.ToString()); this.txtShow.AppendText("\r\n"); break; } } } fc.Show(); th[index].Abort(); }); } } }