来自文本文件的多线程围栏线,然后在收到写入另一个文本文件的肯定响应后发送请求表单 post \ get 到该数据。
public int index = -1;
public int count = 1000;
private static readonly object SyncIndex = new object();
private static readonly object SyncFiles = new object();
public void CreateThreads(int threads)
{
Thread[] threadArray;
for (int i = 0; i < (threadArray = new Thread[threads]).Length; i++)
{
threadArray[i] = new Thread(this.Run) { IsBackground = true };
threadArray[i].Start();
}
}
public void Run()
{
while (true)
{
lock(SyncIndex) { index++;}
//if (index > count) { break; }
string resp = Check(index.ToString());
lock (SyncFiles)
{
if (resp == "true")
{
SaveText("good.txt", index.ToString());
}
else
{
SaveText("bad.txt", index.ToString());
}
}
}
}
public string Check(string login)
{
try
{
System.Net.WebRequest reqGET = System.Net.WebRequest.Create(@"http://www.minecraft.net/haspaid.jsp?user=" + login);
System.Net.WebResponse resp = reqGET.GetResponse();
System.IO.Stream stream = resp.GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(stream);
string s = sr.ReadToEnd();
if (s.Contains("true"))
{
return "true";
}
else
{
return "false";
}
}
catch
{
Check(login);
}
return "";
}
//static ReaderWriterLockSlim cacheLock = new ReaderWriterLockSlim();
private static void SaveText(string file, string text)
{
//cacheLock.EnterWriteLock();
try
{
var write = new StreamWriter(file, true);
write.WriteLine(text);
write.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
//finally
//{
// cacheLock.ExitWriteLock();
//}
}
private void btStart_Click(object sender, EventArgs e)
{
CreateThreads(300);
}
什么是问题?计数 = 1000 个线程 = 300
bad.txt
299
300
301
302
303
304
305
306
307
308
310
311
312
313
314
315
316
good.txt
309
为什么文本从 300 开始?我哪里做错了??????