【发布时间】:2011-10-13 09:31:02
【问题描述】:
我正在使用 c# 编写程序(C# 和面向对象的编程新手) 我正在尝试在多个线程中下载页面。仅运行 1 个线程时没有问题。当我运行多个时,问题就开始了。
似乎所有线程都将下载的信息保存在同一个变量 SockBuff 中,但我不知道如何解决这个问题。
建议?
这里是代码
当我点击按钮时程序启动。
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
void TestaThread_1()
{
Random RandomNumber = new Random();
int rand = RandomNumber.Next(99);
HTTP cURL = new HTTP();
cURL.CurlInit();
String data = cURL.HTTPGet("http://google.com", "", "fails" + rand.ToString() + ".html");
HTTP.save_file("fails" + rand.ToString()+ ".html", data);
}
private void button1_Click(object sender, EventArgs e)
{
Thread thread1 = new Thread(new ThreadStart(TestaThread_1));
thread1.Start();
Thread thread2 = new Thread(new ThreadStart(TestaThread_1));
thread2.Start();
Thread thread3 = new Thread(new ThreadStart(TestaThread_1));
thread3.Start();
Thread thread4 = new Thread(new ThreadStart(TestaThread_1));
thread4.Start();
Thread thread5 = new Thread(new ThreadStart(TestaThread_1));
thread5.Start();
}
}
class HTTP
{
public Easy easy;
public static string SockBuff;
public string CookieFile = AppDomain.CurrentDomain.BaseDirectory + "cookie.txt";
public string UserAgent = "Mozilla 5.0";
public string Proxy = "";
public void CurlInit()
{
Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
}
public string HTTPGet(string URL, string Proxy, String FailaNosaukums)
{
easy = new Easy();
SockBuff = "";
try
{
Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteData);
easy.SetOpt(CURLoption.CURLOPT_URL, URL);
easy.SetOpt(CURLoption.CURLOPT_TIMEOUT, "60");
easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);
easy.SetOpt(CURLoption.CURLOPT_USERAGENT, UserAgent);
easy.SetOpt(CURLoption.CURLOPT_COOKIEFILE, CookieFile);
easy.SetOpt(CURLoption.CURLOPT_COOKIEJAR, CookieFile);
easy.SetOpt(CURLoption.CURLOPT_FOLLOWLOCATION, true);
if (URL.Contains("https"))
{
easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYHOST, 1);
easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, 0);
}
if (Proxy != "")
{
easy.SetOpt(CURLoption.CURLOPT_PROXY, Proxy);
easy.SetOpt(CURLoption.CURLOPT_PROXYTYPE, CURLproxyType.CURLPROXY_HTTP);
}
easy.Perform();
easy.Cleanup();
}
catch
{
Console.WriteLine("Get Request Error");
}
return SockBuff;
}
public static Int32 OnWriteData(Byte[] buf, Int32 size, Int32 nmemb, Object extraData)
{
Random rand = new Random();
int RandomNumber = rand.Next(1000);
SockBuff = SockBuff + System.Text.Encoding.UTF8.GetString(buf);
return size * nmemb;
}
static public void save_file(string file_name, string text_to_write)
{
StreamWriter MyStream = null;
string MyString = text_to_write;
try
{
MyStream = File.CreateText(file_name);
MyStream.Write(MyString);
}
catch (IOException e)
{
Console.WriteLine(e);
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
if (MyStream != null)
MyStream.Close();
}
}
}
}
我还有一个问题。
为什么我不能正确使用 save_file 函数保存图像文件? buff中有编码问题吗?
【问题讨论】: