线程延时为啥使得数据访问又从头开始了那?求高手急救,明天就要交项目了。
我做了个异步下载AP,为保证同时有2个线程下载,做了线程延时,直接用thread.spleep会阻塞异步。如果到达2同时下载个就等待,值得检查到系统有1个或者0个的时候再次继续
队列中有1,2,3,4。下载完ID:1,2的,延时判断CheckOutOfMaxThread()通过再次运行,又从1开始了,为啥不从上次延时的3开始那?
调用代码如下
for (int i = 0; i < count_total;i++ )C# 多线程 下载 线程
{
pic_id = ApplicationCommon.IsInt(dt_gallery.Rows[i]["pic_id"].ToString());
pic_no = dt_gallery.Rows[i]["pic_no"].ToString();
user_account = dt_gallery.Rows[i]["user_account"].ToString();
if (CheckOutOfMaxThread())
{
#region Gallery download
#endregion
}
}
/// <summary>
/// if out of max thread then sleep 1 minute then check again
/// </summary>
/// <returns></returns>
private bool CheckOutOfMaxThread()
{
bool flag_run = false;
int current_count = 0;
current_count = ApplicationCommon.IsInt(lb_download_count.Text);
if (current_count >= MaxThread)
{
writeInfo(current_count + " out of " + MaxThread + " thread ,then sleep 1 minute then to run", Color.Red);
delayTime(60); //sleep 1 minute
CheckOutOfMaxThread();
}
else
{
flag_run = true;
}
flag_run = true;
return flag_run;
}