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

About Ronnie Diaz

Ronnie Diaz is a software engineer and tech consultant. Ronnie started his career in front-end and back-end development for companies in ecommerce, service industries and remote education. This work transitioned from traditional desktop client-server applications through early cloud development. Software included human resource management and service technician workflows, online retail e-commerce and electronic ordering and fulfillment, IVR customer relational systems, and video streaming remote learning SCORM web applications. Hands on server experience and software performance optimization led to creation of a startup business focused on collocated data center services and continued experience with video streaming hardware and software. This led to a career in Amazon Prime Video where Ronnie is currently employed, building software and systems which stream live sports and events for millions of viewers around the world.

Posted on December 13, 2013, in .NET Reference, Programming & Development and tagged , , , , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: