相关疑难解决方法(0)

限制C#中的并行线程数

我正在编写一个C#程序,通过FTP生成并上传50万个文件.我想并行处理4个文件,因为机器有4个核心,文件生成需要更长的时间.是否可以将以下Powershell示例转换为C#?或者是否有更好的框架,如C#中的Actor框架(如F#MailboxProcessor)?

Powershell的例子

$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 …
Run Code Online (Sandbox Code Playgroud)

c# parallel-processing c#-4.0

22
推荐指数
3
解决办法
3万
查看次数

标签 统计

c# ×1

c#-4.0 ×1

parallel-processing ×1