How to get a second System.Thread.ThreadPool?

前端 未结 6 1830
自闭症患者
自闭症患者 2021-01-18 23:16

If I use the ThreadPool in a nested way, my application hangs:

 ThreadPool.QueueUserWorkItem((state) =>
      ThreadPool.QueueUserWorkItem(Action));
         


        
6条回答
  •  南笙
    南笙 (楼主)
    2021-01-18 23:42

    You are using the wrong tools.

    In .NET 4.0 they introduced the Task Parallel Library. This allows you to do things like use multiple thead pools as well as have parent-child relationships between work items.

    Start with the Task class, which replaces ThreadPool.QueueUserWorkItem.

    http://msdn.microsoft.com/en-us/library/system.threading.tasks.task(VS.100).aspx

    EDIT

    Example of creating your own thread pool using Task and TaskScheduler.

    http://msdn.microsoft.com/en-us/library/dd997413(VS.100).aspx

提交回复
热议问题