The Task Parallel Library explains the concept of "Task". It executes Tasks in parallel way. It has great advantages over thread and thread pool. This library has been released in version 4.0 of the .NET framework.
The following are some benefits of using Tasks:
- It uses system resources more efficiently.
- It provides more programming control than threads.
- It also includes other concept like Future. Future returns result.
Namespace
- using System.Threading.Tasks
1. Direct and common way
2. Task using with Action
3. Task using Delegates
4. Tasks using Lambda and named method:
5. Task using Lambda and anonymous method:
6. Creating and running Tasks Implicitly:
- #region Most Direct Way
- /// <summary>
- /// Most direct way to create thread.
- /// </summary>
- public void MostDirectWay()
- {
- Task.Factory.StartNew(() => { Console.WriteLine("Most Direct and Common way to create thread."); });
- }
- #endregion
- #region Thread using Action
- public void ThreadUsingAction()
- {
- Task task = new Task(new Action(PrintMessage));
- task.Start();
- }
- private void PrintMessage()
- {
- Console.WriteLine("Thread using Action.");
- }
- #endregion
- #endregion
- #region Thread using Delegate
- public void ThreadUsingDelegate()
- {
- Task task = new Task(delegate { PrintMessageForDelegateThread(); });
- task.Start();
- }
- private void PrintMessageForDelegateThread()
- {
- Console.WriteLine("Task Creation using Delegate");
- }
- #endregion
- #region Thread using Lambda and named method
- public void ThreadUsingLambda()
- {
- Task task = new Task(() => PrintMessageMessageForLambda());
- task.Start();
- }
- private void PrintMessageMessageForLambda()
- {
- Console.WriteLine("Task with Lambda");
- }
- #endregion
- #region Lambda and anonymous method:
- public void TaskUsingLambdaAndAnonymousMethod()
- {
- Task task = new Task(() => Console.WriteLine("Task with Anonymous Method"));
- task.Start();
- }
- #endregion
Tasks are created and maintained implicitly by the framework. It is used when developer needs to process Tasks which do not return any value and no more control is required.
The Parallel.Invoke method provides a best way to run any number of arbitrary statements concurrently. It creates Tasks implicitly.
- #region Create Task implicitly using Parallel.Invoke
- public void ImplicitTaskWithParallel()
- {
- Parallel.Invoke(() => DoSomeWork(), () => DoSomeOtherWork());
- }
- private void DoSomeOtherWork()
- {
- Console.WriteLine("Execute second Tasks");
- }
- private void DoSomeWork()
- {
- Console.WriteLine("Execute First Task");
- }
- #endregion
Santhakumar MunuswamyPosted Oct 18, 2015, 3:52 AM
Good one
Turker TunaliPosted Oct 13, 2015, 1:56 AM
Nice article but it would be better if you also specify initialization of tasks with parameters. Then it would be a poster.
Yashwanth MuthineniPosted Oct 13, 2015, 12:45 AM
Nice share
Former memberPosted Oct 12, 2015, 1:31 PM
Thanks to all.
Mohammed IbrahimPosted Oct 12, 2015, 12:09 PM
nice
RakeshPosted Oct 12, 2015, 11:39 AM
Good share
Mukesh KumarPosted Oct 12, 2015, 8:13 AM
Great, thanks for sharing
Sujeet SumanPosted Oct 12, 2015, 7:17 AM
Great thing..............
Sibeesh VenuPosted Oct 12, 2015, 3:12 AM
Nice Share