运行多个线程,在另一个线程完成时启动新线程
我有一个包含很多案例的应用程序。每个案例都有许多多页 tif 文件。我需要将 tf 文件转换为 pdf 文件。由于文件太多,我想我可以线程化转换过程。我目前将该过程限制为一次十次转换(即十个步骤)。当一个转换完成后,另一个转换应该开始。
这是我当前使用的设置。
private void ConvertFiles()
{
List<AutoResetEvent> semaphores = new List<AutoResetEvet>();
foreach(String fileName in filesToConvert)
{
String file = fileName;
if(semaphores.Count >= 10)
{
WaitHandle.WaitAny(semaphores.ToArray());
}
AutoResetEvent semaphore = new AutoResetEvent(false);
semaphores.Add(semaphore);
ThreadPool.QueueUserWorkItem(
delegate
{
Convert(file);
semaphore.Set();
semaphores.Remove(semaphore);
}, null);
}
if(semaphores.Count > 0)
{
WaitHandle.WaitAll(semaphores.ToArray());
}
}
使用此方法有时会导致异常,指出 WaitHandle.WaitAll() 或 WaitHandle.WaitAny() 数组参数的长度不得超过 65。我在这种方法中做错了什么以及如何纠正它?
I have an application that has many cases. Each case has many multipage tif files. I need to covert the tf files to pdf file. Since there are so many file, I thought I could thread the conversion process. I'm currently limiting the process to ten conversions at a time (i.e ten treads). When one conversion completes, another should start.
This is the current setup I'm using.
private void ConvertFiles()
{
List<AutoResetEvent> semaphores = new List<AutoResetEvet>();
foreach(String fileName in filesToConvert)
{
String file = fileName;
if(semaphores.Count >= 10)
{
WaitHandle.WaitAny(semaphores.ToArray());
}
AutoResetEvent semaphore = new AutoResetEvent(false);
semaphores.Add(semaphore);
ThreadPool.QueueUserWorkItem(
delegate
{
Convert(file);
semaphore.Set();
semaphores.Remove(semaphore);
}, null);
}
if(semaphores.Count > 0)
{
WaitHandle.WaitAll(semaphores.ToArray());
}
}
Using this, sometimes results in an exception stating the WaitHandle.WaitAll() or WaitHandle.WaitAny() array parameters must not exceed a length of 65. What am I doing wrong in this approach and how can I correct it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你写的东西有一些问题。
第一,它不是线程安全的。您有多个线程添加、删除和等待 AutoResetEvents 数组。 List 的各个元素可以在单独的线程上访问,但是添加、删除或检查所有元素的任何操作(如 WaitAny 调用)都需要在锁内执行。
第二,不能保证您的代码一次只能处理 10 个文件。检查列表大小和添加新项目之间的代码是开放的,可供多个线程通过。
第三,QueueUserWorkItem 中启动的线程有可能转换同一文件。如果不在循环内捕获 fileName,转换文件的线程将在执行时使用 fileName 中的任何值,而不是调用 QueueUserWorkItem 时 fileName 中的任何值。
这篇 codeproject 文章应该为您指明要做的事情的正确方向: http: //www.codeproject.com/KB/threads/SchedulingEngine.aspx
编辑:
就我个人而言,我不认为我会这样做......但是,使用您拥有的代码,这应该可以工作。
There are a few problems with what you have written.
1st, it isn't thread safe. You have multiple threads adding, removing and waiting on the array of AutoResetEvents. The individual elements of the List can be accessed on separate threads, but anything that adds, removes, or checks all elements (like the WaitAny call), need to do so inside of a lock.
2nd, there is no guarantee that your code will only process 10 files at a time. The code between when the size of the List is checked, and the point where a new item is added is open for multiple threads to get through.
3rd, there is potential for the threads started in the QueueUserWorkItem to convert the same file. Without capturing the fileName inside the loop, the thread that converts the file will use whatever value is in fileName when it executes, NOT whatever was in fileName when you called QueueUserWorkItem.
This codeproject article should point you in the right direction for what you are trying to do: http://www.codeproject.com/KB/threads/SchedulingEngine.aspx
EDIT:
Personally, I don't think I'd do it this way...but, working with the code you have, this should work.
您是否使用真正的信号量(System.Threading )?使用信号量时,您通常会分配最大资源,并且它会自动为您阻塞(当您添加和释放时)。您可以采用 WaitAny 方法,但我感觉您选择了更困难的路线。
Are you using a real semaphore (System.Threading)? When using semaphores, you typically allocate your max resources and it'll block for you automatically (as you add & release). You can go with the WaitAny approach, but I'm getting the feeling that you've chosen the more difficult route.
看起来您需要删除触发 WaitAny 函数的句柄才能继续
删除:调用并使用上面的内容来删除有信号的事件并查看是否有效。
所以基本上我会从线程中
Looks like you need to remove the handle the triggered the WaitAny function to proceed
So basically I would remove the:
call from the thread and use the above to remove the signaled event and see if that works.
也许您不应该创建这么多事件?
Maybe you shouldn't create so many events?