Thursday, 5 September 2013

How to implement concurrent calls in C# console app, .NET 4.5?

How to implement concurrent calls in C# console app, .NET 4.5?

I'm polling an external queue for job requests, then process the job
(which takes ~1 minute). Currently, I'm doing it synchronously, which is
inefficient because if there were 10 jobs, it would take 10 minutes. The
machine can handle up to 5 jobs at the same time, so it should be able to
process 10 jobs in ~2 minutes.
I've never done multi-threading, I've tried to read about async, await,
Task.Run, but haven't had any success implementing it. Any suggestions on
a simple way to implement this pseudocode?
while(concurrentJobs <= 5)
{
job = pollForJob(); // synchronous
processJob(job); // want to do this in background so that I can poll
for next job
}
void processJob(job)
{
concurrentJobs++;
doStuff(); // takes 1 minute
concurrentJobs--;
}

No comments:

Post a Comment