Blog Archives

TPL Task Parrallel Library .net 4.0 parallel programming multi thread threading task queue user work item

//simple parallel function call
Parallel.Invoke(() => somefunc1(), () => somefunc2());

//parallel foreach with two lists
new List<List> tasks = new List<List>()
{
  new List() { new DataSource1ProcessorA() },
  new List() { new DataSource2ProcessorA(), new DataSource2ProcessorB() }
}

Parallel.ForEach(tasks, (items) =>
{
    foreach (var item in items)
    {
        item.GetData();
        item.Process();
    }
});

//new task static factory method approach
Task.Factory.StartNew(() => { data.GetData(); data.Process(); })
.ContinueWith(t => Logger.Error("An exception occurred while processing. Check the inner exception for details", t.Exception),
TaskContinuationOptions.OnlyOnFaulted);

//explicit object oriented
   // Create a task and supply a user delegate by using a lambda expression. 
        Task taskA = new Task( () => Console.WriteLine("Hello from taskA."));
        // Start the task.
        taskA.Start();
        // Output a message from the calling thread.
        Console.WriteLine("Hello from thread '{0}'.", 
                          Thread.CurrentThread.Name);
        taskA.Wait();

//using task run
  Thread.CurrentThread.Name = "Main";
      // Define and run the task.
      Task taskA = Task.Run( () => Console.WriteLine("Hello from taskA."));
      // Output a message from the calling thread.
      Console.WriteLine("Hello from thread '{0}'.", 
                          Thread.CurrentThread.Name);
      taskA.Wait();

References
http://msdn.microsoft.com/en-us/library/dd460705.aspx

(added 20121023 good comprehensive resource with illustrative examples)
http://www.codeproject.com/Articles/362996/Multi-core-programming-using-Task-Parallel-Library

http://www.codeproject.com/KB/threads/ParallelTasks.aspx

Optimize Managed Code For Multi-Core Machines
http://msdn.microsoft.com/en-us/magazine/cc163340.aspx

good blog
http://www.codethinked.com/net-40-and-systemthreadingtasks

queueuserworkitem
http://msdn.microsoft.com/en-us/library/4yd16hza.aspx

threadpool class
http://msdn.microsoft.com/en-us/library/y5htx827.aspx

task class
http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx

task parrallelism
http://msdn.microsoft.com/en-us/library/dd537609.aspx

How to: Use Parallel.Invoke to Execute Parallel Operations
http://msdn.microsoft.com/en-us/library/dd460705.aspx

Advertisement