I'm writing an application where I have a class Rechnungsleser
with 2 methods and some fields.
Fields:
invoices = new BlockingCollection<Invoice>(new ConcurrentQueue<Invoice>());
// Total number of invoices in the XML
invoicesToProcess = XDocument.Load(sourceFile).Root.Elements().Count();
// Number of invoices consumed
invoicesProecessed = 0;
public void ReadRechnungslauf() (below)
using (XmlReader rechnungsreader = XmlReader.Create(sourceFile))
{
// Iterate over whole file
while (!rechnungsreader.EOF)
{
if (someCondition)
{
XElement rechnung = XNode.ReadFrom(rechnungsreader) as XElement;
// Some minor change
Helper.RemoveAllNamespaces(rechnung);
if (rechnung != null)
{
try
{
// Decent change, takes XElement, reads and changes some Elements, return object of type Invoice
invoices.Add(CreateRechnung(rechnung, possibleOverlays));
}
catch (Exception e)
{
Log(e)
throw;
}
}
}
else
{
rechnungsreader.Read();
}
}
}
public void ProcessRechnungslauf()
{
Rechnung currentRechnung;
// Check that all invoices are processed
while (invoicesToProcess != invoicesProecessed)
{
// Take next enqueued item
currentRechnung = rechnungen.Take();
if (currentRechnung.PrintMode.Equals("Online"))
{
// Very computing heavy method
printer.PrintRechnung(currentRechnung);
}
invoicesProcessed++;
}
}
I initialize both methods like this:
using (Rechnungsleser reader = new Rechnungsleser()
{
Task readingTask = Task.Factory.StartNew(() =>
{
reader.ReadRechnungslauf();
});
Task processingTask = Task.Factory.StartNew(() =>
{
reader.ProcessRechnungslauf();
});
Task.WaitAll(readingTask, processingTask);
}
When I check the log it first reads all invoices and places them in the queue and then starts processing them. How can I ensure the parallelism? Is it only because ProcessRechnungslauf()
needs much more performance then ReadRechnungslauf()
?
Should I use Threads
or the TaskParallelLibrary
? Why?
My understanding is, that a Task
takes an available Thread
from Threadpool
and executes it. I don't see why these 2 Tasks shouldn't work together.