首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

线程延时为何使得数据访问又从头开始了那?求高手急救,明天就要交项目了

2013-08-01 
线程延时为啥使得数据访问又从头开始了那?求高手急救,明天就要交项目了。我做了个异步下载AP,为保证同时有2

线程延时为啥使得数据访问又从头开始了那?求高手急救,明天就要交项目了。
我做了个异步下载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++ )
                        {
                            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;
        }

C# 多线程 下载 线程
[解决办法]
其实是你的思路有问题
你是可以确定dt_gallery.Rows.Count的
根据这个,一开始你就可以先开两个线程,每个线程处理的机制相同
1.线程开始处理时,先将处理的Row数据对象标记正在处理..
2.处理完之后,标记为处理完毕
3.遍历Rows,取得第一个还未处理的Row,转到步骤1. 如果所有的都已处理完毕,则返回(线程结束)

这样就不必去判断线程的数量了

热点排行