限制在C#并行线程的数目 [英] Limit the number of parallel threads in C#

查看:96
本文介绍了限制在C#并行线程的数目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个C#程序产生,并通过FTP上传一个半百万个文件。欲并行处理4个文件,因为机器具有4芯和文件生成花费更长的时间。是否有可能为以下Powershell的例子转换为C#?或者是还有什么更好的框架,如C#(如F#MailboxProcessor)?

I am writing a C# program to generate and upload a half million files via FTP. I want to process 4 files in parallel since the machine have 4 cores and the file generating takes much longer time. Is it possible to convert the following Powershell example to C#? Or is there any better framework such as Actor framework in C# (like F# MailboxProcessor)?

<一个href=\"http://blogs.msdn.com/b/powershell/archive/2011/04/04/scaling-and-queuing-powershell-background-jobs.aspx\"相对=nofollow> Powershell的例子

Powershell example

$maxConcurrentJobs = 3;

# Read the input and queue it up
$jobInput = get-content .\input.txt
$queue = [System.Collections.Queue]::Synchronized( (New-Object System.Collections.Queue) )
foreach($item in $jobInput)
{
    $queue.Enqueue($item)
}

# Function that pops input off the queue and starts a job with it
function RunJobFromQueue
{
    if( $queue.Count -gt 0)
    {
        $j = Start-Job -ScriptBlock {param($x); Get-WinEvent -LogName $x} -ArgumentList $queue.Dequeue()
        Register-ObjectEvent -InputObject $j -EventName StateChanged -Action { RunJobFromQueue; Unregister-Event $eventsubscriber.SourceIdentifier; Remove-Job $eventsubscriber.SourceIdentifier } | Out-Null
    }
}

# Start up to the max number of concurrent jobs
# Each job will take care of running the rest
for( $i = 0; $i -lt $maxConcurrentJobs; $i++ )
{
    RunJobFromQueue
}

更新:结果
到远程FTP服务器的连接可能会很慢,所以我想限制FTP上传处理。

Update:
The connection to remote FTP server can be slow so I want to limit the FTP uploading processing.

推荐答案

假设你正在与第三方物流建立这一点,您可以设置<一个href=\"http://msdn.microsoft.com/en-us/library/system.threading.tasks.paralleloptions.maxdegreeofparallelism.aspx\">ParallelOptions.MaxDegreesOfParallelism任何你希望它是。

Assuming you're building this with the TPL, you can set the ParallelOptions.MaxDegreesOfParallelism to whatever you want it to be.

的Parallel.For 为code的例子。

这篇关于限制在C#并行线程的数目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆