public void BtGongchangWork(object obj) { #region 步版本的实现思路 //List<string> ls = obj as List<string>; //Random rm = new Random(); //for (int i = 0; i < ls.Count; i++) //{ // BTFuzhuaClass btLink = new BTFuzhuaClass(ls[i]); // string dir = "E:\\BT\\"; // string filename = i + "-" + btLink.GetBTData_Filename(); // string code = RequestPost(btLink.GetBTData_PostURL(), ls[i], btLink.GetBTData_PostData(), dir + filename + ".torrent", null, 1); // if (thGongchang != null) // { // this.Invoke(new Action<string>(UpdateUI), code); // } // this.Invoke(new Action<string>(UpdateUI), i + "成功\n"); // if (thGongchang != null) // { // this.Invoke(new Action<string>(UpdateUI), "第一次休息中...\n"); // thGongchang.Join(rm.Next(400, 900)); // } //} #endregion #region 异步版本的实现思路 MyBTObjectInfo bt = (MyBTObjectInfo)obj; List <string> ls = bt.Lbtlist; Random rm = new Random(); for (int i = 0; i < ls.Count; i++) { try { BTFuzhuaClass btLink = new BTFuzhuaClass(ls[i]); string dir = bt.strFloder; string filename = i + "-" + btLink.GetBTData_Filename(); RequestPostAsync(btLink.GetBTData_PostURL(), ls[i], btLink.GetBTData_PostData(), dir + filename + ".torrent", null); this.Invoke(new Action <string>(UpdateUI), i + "-成功\n"); if (thGongchang != null) { //this.Invoke(new Action<string>(UpdateUI), "第一次休息中...\n"); } } catch { continue; } } #endregion }
Thread thGongchang;//最大支持100个 private void btBTGongchang_Click(object sender, EventArgs e) { //采用的是多线程 开启异步的方式 MyBTObjectInfo b = new MyBTObjectInfo(this.afile, this.myStringFloder); //先创建出存放BT的目录 b.CreateFloder(); //MessageBox.Show(b.strFloder); //改线程执行一个带有参数的委托 thGongchang = new Thread(new ParameterizedThreadStart(BtGongchangWork)); //线程开始的时候调用对象的方法 thGongchang.Start(b); }