//开始获取线程
private void btn_start_Click(object sender, EventArgs e)
{
if (tCompare != null)
{
tCompare.Start();
}
else
{
tCompare = new Thread(new ThreadStart(compare));
tCompare.IsBackground = true;
tCompare.Start();
}
}
//无限循环,当到达指定的时间点后(每天),便开始执行线程(从网页抓取数据)
public void compare()
{
while (1 == 1)
{
Application.DoEvents();
System.DateTime currentTime = new System.DateTime();
currentTime = System.DateTime.Now;
int iHour = currentTime.Hour;//当前 时
int iMinute = currentTime.Minute;//当前 分
int iSecond = currentTime.Second;// 当前 秒 int xHour = int.Parse(tb_hour.Text);//设定 时
int xMinute = int.Parse(tb_minute.Text);//设定 分
int xSecond = int.Parse(tb_second.Text);//设定 秒
if (iHour == xHour && iMinute == xMinute && iSecond == xSecond)//到达指定时间,启动4个线程
{
currcount = 0;
if (tGetHtml1 != null)
{
tGetHtml1.Start();
}
else
{
tGetHtml1 = new Thread(new ThreadStart(GetHtml));
tGetHtml1.IsBackground = true;
tGetHtml1.Start();
System.Threading.Thread.Sleep(200);
} if (tGetHtml2 != null)
{
tGetHtml2.Start();
}
else
{
tGetHtml2 = new Thread(new ThreadStart(GetHtml));
tGetHtml2.IsBackground = true;
tGetHtml2.Start();
System.Threading.Thread.Sleep(200);
} if (tGetHtml3 != null)
{
tGetHtml3.Start();
}
else
{
tGetHtml3 = new Thread(new ThreadStart(GetHtml));
tGetHtml3.IsBackground = true;
tGetHtml3.Start();
System.Threading.Thread.Sleep(200);
} if (tGetHtml4 != null)
{
tGetHtml4.Start();
}
else
{
tGetHtml4 = new Thread(new ThreadStart(GetHtml));
tGetHtml4.IsBackground = true;
tGetHtml4.Start();
System.Threading.Thread.Sleep(200);
} }
else
{
Thread.Sleep(1000);
}
}
}
//从网页抓取数据
public void GetHtml()
{
//抓取过程
break;
}//退出
private void FlightGet_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("确定关闭?", "关闭确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
{
e.Cancel = true;
}
else
{
if (tCompare != null)
{
tCompare.Abort();
}
if (tGetHtml1 != null)
{
tGetHtml1.Abort();
}
if (tGetHtml2 != null)
{
tGetHtml2.Abort();
}
if (tGetHtml3 != null)
{
tGetHtml3.Abort();
}
if (tGetHtml3 != null)
{
tGetHtml4.Abort();
}
}
}
说明:实际上,是开启了10个线程去取数据。由于数据量比较大,所以必须用多个线程。现在的情况是:程序可以执行一次,可以正常关闭,但是无法实现循环执行,也就是在第二次到达指定的时间时,程序会出错。怎么解决呢?望各位高手不吝赐教。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货