English 中文(简体)
在不同的线程中处理列表项
原标题:process list items in different threads

当时,我有按顺序排列的待处理工作对象名单,载于《法典》的表格。

   List<Job> jobList = jobQueue.GetJobsWithStatus(Status.New);
   foreach (Job job in jobList)
   {               
        job.Process();
   }

我有兴趣在有限的线程数量(比如说5个线程)中同时运行几个作业。

用C#做这个的最佳方法是什么?

http://www.ohchr.org。

  • A Job object does not share resources with other jobs.
  • Each Job takes about 10 seconds to process.
  • Each job could connect to different resources.

更新:我使用了信号量,因为我不能使用线程池限制活动线程的数量。

最佳回答

如果你想冒险一试,你可以使用C# 4.0和任务并行库:

Parallel.ForEach(jobList, curJob => {
  curJob.Process()
});
问题回答

你们将想看一看一线(简单回答)。 在C#中甚至有一个ThreadPool级,很容易在

线程池.队列用户工作项()

这将排队一个方法以等待执行,它将在线程池线程可用时执行。

我将尝试使用.NET线程池。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签