c# 4.0 - Limit the number of parallel threads in C# -
c# 4.0 - Limit the number of parallel threads in C# -
i writing c# programme generate , upload half 1000000 files via ftp. want process 4 files in parallel since machine have 4 cores , file generating takes much longer time. possible convert next powershell illustration c#? or there improve framework such actor framework in c# (like f# mailboxprocessor)?
powershell example
$maxconcurrentjobs = 3; # read input , queue $jobinput = get-content .\input.txt $queue = [system.collections.queue]::synchronized( (new-object system.collections.queue) ) foreach($item in $jobinput) { $queue.enqueue($item) } # function pops input off queue , starts job 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 max number of concurrent jobs # each job take care of running rest for( $i = 0; $i -lt $maxconcurrentjobs; $i++ ) { runjobfromqueue }
update: connection remote ftp server can slow want limit ftp uploading processing.
assuming you're building tpl, can set paralleloptions.maxdegreesofparallelism whatever want be.
parallel.for code example.
c# c#-4.0 parallel-processing
Comments
Post a Comment