Limit Threads count

前端 未结 4 2073
清歌不尽
清歌不尽 2021-01-01 03:22

I have a List with items that I want to download. I use a for Loop to iterate the list.

For each item in this List I start a new Thread that references the item. My

4条回答
  •  情歌与酒
    2021-01-01 03:53

    if you're using .NET 4, I'd strongly suggest using Parallel.ForEach (potentially on downloadList.Reverse())

    so, something like:

    Parallel.ForEach(downloadList.Reverse(), 
                     new ParallelOptions { MaxDegreeOfParallelism = 8 },
                     item => this.DownloadItem(item));
    

    If you don't want the calling thread to block, you could QueueUserWorkItem this call, of course.

提交回复
热议问题