currentrequestId++;
int a = currentrequestId;
RequestIds.Add(currentrequestId, true);
List<Task> TaskList = new List<Task>();
for (int i = 0; i < users.Length; i++)
{
int l = i;
for (int y = 0; y < passwords.Length; y++)
{
int Y = y;
TaskList.Add(Task.Factory.StartNew(() =>
{
if (RequestIds[a])
{
bool result = FTPRequest(host, users[l], passwords[Y]);
if (result)
{
BeginInvoke(new Action(() =>
{
if (RequestIds[a])
{
if(checkBox1.Checked)
{
RequestIds[a] = false;
richTextBox3.Text += "爆破成功!用户名:" + users[l] + " 密码:" + passwords[Y] + "\r";
button3.Text = "开始爆破";
label2.Text = "";
richTextBox3.Text += "爆破结束!";
textBox1.Enabled = true;
button1.Enabled = true;
button2.Enabled = true;
radioButton1.Enabled = true;
radioButton2.Enabled = true;
radioButton3.Enabled = true;
checkBox1.Enabled = true;
MessageBox.Show("爆破结束!用户名:" + users[l] + "密码:" + passwords[Y], "提示");
}
else
{
richTextBox3.Text += "爆破成功!用户名:" + users[l] + " 密码:" + passwords[Y] + "\r";
}
}
}));
}
else
{
BeginInvoke(new Action(() =>
{
if (RequestIds[a])
{
richTextBox3.Text += "正在爆破!当前用户名:" + users[l] + " 当前密码:" + passwords[Y] + "\r";
}
}));
}
}
else
{
return;
}
}));
}
整个程序的源码在一个Github仓库:https://github.com/7hr0wer/SuperBuster
所以我现在的思路就是像ThreadPool类的SetMaxThread一样设置一个最大的Task并行数量,我也通过搜索引擎查了,网上大多都是用 LimitedConcurrencyLevelTaskScheduler实现的限制,
我按照自己的理解,也就是说
TaskFactory fac = new TaskFactory(new LimitedConcurrencyLevelTaskScheduler(5));里面的这个5是是设置的最大Task并发数量,按照代码实例重写了,可是仍然没有任何变化。
我不知道是不是我的理解有误!所以请问有没有大佬知道Task如何设置最大并行Task数量?如果我的理解有误,能否指出我的错误并且纠正呢?
如果大佬们能给出有代码注释的Demo那就更好了!谢谢!
TaskFactory fac = new TaskFactory(new LimitedConcurrencyLevelTaskScheduler(5));