public void Start()
{
this.m_hWorkThreadRang = new Thread(new ThreadStart(this.Fun1));
this.m_hWorkThreadRang.IsBackground = true;
this.m_hWorkThreadRang.Start();
this.m_hWorkThreadZoudi = new Thread(new ThreadStart(this.Fun2));
this.m_hWorkThreadZoudi.IsBackground = true;
this.m_hWorkThreadZoudi.Start();
}
private void Fun1()
{
string strData = "";
int num2 = 0;
DateTime now = DateTime.Now;
while (true)
{
try
{
TimeSpan span = (TimeSpan)(DateTime.Now - now); //每隔6秒抓一次
num2 =6000 - ((int)span.TotalMilliseconds);
}
catch (Exception)
{
num2 = 300;
}
now = DateTime.Now;
if (num2 <= 0)
{
num2 = 300;
}
Thread.Sleep(num2);
try
{
http d=new http();
d.Cookie = this.m_strCookie;
d.URL = this.a.hosturl + "/data.aspx?Market=t";
d.Referer = this.a.hosturl;
d.Command = "GET"; strData=d.Getit();
Util.WriteIniappends("sb.txt", string.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now)+" 地址1抓取!");
}
catch (Exception exception)
{
}
}
} private void Fun2()
{
string strData = "";
DateTime now = DateTime.Now;
int num;
while (true)
{
try
{
TimeSpan span = (TimeSpan)(DateTime.Now - now);
//隔两秒抓一次
num = 2000 - ((int)span.TotalMilliseconds);
}
catch (Exception)
{
num = 100;
}
now = DateTime.Now;
if (num <= 0)
{
num = 100;
}
Thread.Sleep(num);
try
{
http d = new http();
d.Cookie = this.m_strCookie;
d.URL = this.a.hosturl + "/data.aspx?Market=l";
d.Referer = this.a.hosturl;
d.Command = "GET";
strData = d.Getit();
Util.WriteIniappends("sb.txt", string.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now) + " 地址2抓取!");
}
catch (Exception exception)
{
}
}
}
当我运行start()方法时,出现问题,FUN1两次数据抓取都没有间隔6秒,FUN2也没有间隔2秒,为什么?第二个问题就是为什么两个线程没有按我设想的顺序抓取,有点杂乱无章的感觉,记录的结果如下:2011-05-31 10:02:01 地址1抓取!
2011-05-31 10:02:01 地址2抓取!
2011-05-31 10:02:05 地址2抓取!
2011-05-31 10:02:05 地址1抓取!
2011-05-31 10:02:05 地址2抓取!
2011-05-31 10:02:07 地址2抓取!
2011-05-31 10:02:07 地址1抓取!
2011-05-31 10:02:08 地址2抓取!
2011-05-31 10:02:10 地址2抓取!
2011-05-31 10:02:10 地址2抓取!
2011-05-31 10:02:12 地址1抓取!
2011-05-31 10:02:12 地址2抓取!
2011-05-31 10:02:12 地址2抓取!
2011-05-31 10:02:14 地址1抓取!
2011-05-31 10:02:14 地址2抓取!
2011-05-31 10:02:14 地址2抓取!
2011-05-31 10:02:16 地址2抓取!
2011-05-31 10:02:17 地址2抓取!
2011-05-31 10:02:18 地址1抓取!
2011-05-31 10:02:19 地址2抓取!
2011-05-31 10:02:19 地址2抓取!
2011-05-31 10:02:20 地址1抓取!
2011-05-31 10:02:21 地址2抓取!
2011-05-31 10:02:21 地址2抓取!
2011-05-31 10:02:23 地址2抓取!
2011-05-31 10:02:23 地址2抓取!
2011-05-31 10:02:24 地址1抓取!
2011-05-31 10:02:25 地址2抓取!
2011-05-31 10:02:26 地址2抓取!
2011-05-31 10:02:26 地址1抓取!
2011-05-31 10:02:28 地址2抓取!
2011-05-31 10:02:28 地址2抓取!
2011-05-31 10:02:30 地址2抓取!
2011-05-31 10:02:30 地址2抓取!
2011-05-31 10:02:30 地址1抓取!
其实要按顺序执行,何必分两个线程,而且同样的方法还写了两次...
private void Fun1()
{
string strData = "";
while (true)
{
Thread.Sleep(6000);
try
{
http d=new http();
d.Cookie = this.m_strCookie;
d.URL = this.a.hosturl + "/data.aspx?Market=t";
d.Referer = this.a.hosturl;
d.Command = "GET"; strData=d.Getit();
Util.WriteIniappends("sb.txt", string.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now)+" 地址1抓取!");
}
catch (Exception exception)
{
}
}
}
AutoResetEvent are1 = new AutoResetEvent(false);
AutoResetEvent are2 = new AutoResetEvent(false);
f1()//方法一
{
while(true)
{
are1.WaitOne();
//TODO:执行操作
are2.Set();
}
}
f2()//方法二
{
while(true)
{
are2.WaitOne();
//TODO:执行操作
are1.Set();
}
}
在线程Start之后手动调用are1.Set() 就可以让f1()方法先执行了,然后,,然后
第一个问题时间间隔
now = DateTime.Now;
if (num2 <= 0)
{
num2 = 300;
}
Thread.Sleep(num2);
now = DateTime.Now应该放在Thread.Sleep(num2);之后Thread.Sleep(num2);
now = DateTime.Now;第二个问题,不知道你设想的什么样。可以用两个事件彼此制约,但要小心死锁。