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

判断所有子线程执行完,教小弟我上,多谢

2013-03-04 
判断所有子线程执行完,教我下,谢谢本帖最后由 skynq 于 2013-02-25 11:19:20 编辑主函数里面static void M

判断所有子线程执行完,教我下,谢谢
本帖最后由 skynq 于 2013-02-25 11:19:20 编辑 主函数里面

static void Main(string[] args)
 {
     //我开了10个线程

    


      for (int i = 0; i < 10; i++)
                    {
                        Thread th = new Thread(() =>
                        {
                            for (int j = 0; j < htmlUrl.Count; j++)
                            {
                                getCount(htmlUrl[j]);
                                htmlUrl.Remove(htmlUrl[j]);   //清空

                            }
                        });

                        th.Start();
                    }
     

}

当然主函数里面还有其他调用的方法。我怎么判断这些子线程全部结束在执行我其他的方法。谢谢。
[解决办法]
试试fastCSharp,供参考
            using (fastCSharp.threading.task task = new fastCSharp.threading.task(10, true))
            {
                for (int i = 0; i < 10; i++)
                {
                    task.Add(() =>
                    {
                        //do something
                    });
                }
            }
            Console.WriteLine("线程执行完毕");


[解决办法]
static void Main()
         {
             PExecuteMethod();
             Console.ReadLine();
         }
        private static void PExecuteMethod()
        {
            Parallel.For(0, 10, (i) =>
            {
                //do your job here                 
                DealWithHtmlUrl(i);
            });
            Console.WriteLine("任务执行完毕.");

            TodoTheLastMethod();
        }

        private static void TodoTheLastMethod()
        {
            //The Last Method todo here
            Console.WriteLine("The Last Method todo here");
        }

        private static void DealWithHtmlUrl(int i)
        {
            //throw new NotImplementedException();

            //todo: 针对第i个url的处理
            Console.WriteLine("针对第{0}个url的处理", i);
        }

热点排行
Bad Request.