C # Task Parallel library using subtotal Z

Source: Internet
Author: User

1. Simple creation using

Using System;
Using System.Diagnostics;
Using System.Threading;
Using System.Threading.Tasks;
Namespace ConsoleApplication17
{
    Class Program
    {
        void Main (string[] args)
        {
            Try
            {
                /*
                 *  
                 * For performance reasons,  the StartNew method of TaskFactory should be the preferred mechanism for creating and scheduling compute tasks, but for scenarios where creation and scheduling must be separate,
                 *  You can use the constructor, and then you can use the Start method of the task to schedule the task to execute later.
                 *  for operations that return values, the Task<tresult> class should be used. ———— MSDN
                 */
                New Task (() = {Console.WriteLine ("My frist Task.");});
                _task. Start ();
                Task _taskfactory = Task.Factory.StartNew (() = {Console.WriteLine ("My frist Task by Factory.") ;
            }
            catch (Exception ex)
            {
                Console.WriteLine (String. Format ("Exception message:{0}", ex. Message.trim ())); 
            }
            Finally
            {
                Console.ReadLine ();
            }
        }
    }
}

Code effects

2.Task Processing return value

Using System;
Using System.Diagnostics;
Using System.Threading;
Using System.Threading.Tasks;
Namespace ConsoleApplication17
{
    Class Program
    {
        void Main (string[] args)
        {
            Try
            {
                task<string> _task = task<string>. Factory.startnew (() = 
                {
                    String _guid = System.Guid.NewGuid (). ToString ();
                    Console.WriteLine (String. Format ("Pass value:{0}", _guid)); 
                    return _guid;
                });
                Console.WriteLine (String. Format ("Task Return value:{0}", _task. Result)); 
            }
            catch (Exception ex)
            {
                Console.WriteLine (String. Format ("Exception message:{0}", ex. Message.trim ())); 
            }
            Finally
            {
                Console.ReadLine ();
            }
        }
    }
}

Code effects

3.Task Task Continuation

Using System;
Using System.Diagnostics;
Using System.Threading;
Using System.Threading.Tasks;
Namespace ConsoleApplication17
{
    Class Program
    {
        void Main (string[] args)
        {
            Try
            {
                /*
                 
                 * The delegate of the continuation task will pass in a reference to the previous task so that it can check its status.
                 * In addition, you can pass a user-defined value from the previous task to its continuation in the Result property.
                 * so that the output of the previous task can be used as input to the continuation task. ———— MSDN
                 */
                task<string> _task = task<string>. Factory.startnew (() = 
                {
                    "_task";
                    Console.WriteLine (_GUID);
                    return _guid;
                }). continuewith<string> (GUID)/=
                {
                    "_task ContinueWith";
                    Console.WriteLine (String. Format ("ContinueWith Task {0}", GUID. Result)); 
                    return _guid;
                });
                Console.WriteLine (String. Format ("Task Return value:{0}", _task. Result)); 
            }
            catch (Exception ex)
            {
                Console.WriteLine (String. Format ("Exception message:{0}", ex. Message.trim ())); 
            }
            Finally
            {
                Console.ReadLine ();
            }
        }
    }
}

Code effects

4. Detached nested tasks

Using System;Using System.Diagnostics;Using System.Threading;Using System.Threading.Tasks;Namespace consoleapplication17{Class Program {Staticvoid Main (String[] (args) {try {/* * If the user code running in the task creates a new task,  * and unspecified AttachedToParent option, the new task is not synchronized with the external task in any special way.  Such tasks are called "detached nested tasks." ———— msdn *   */Task _outer = Task.Factory.StartNew (() = {Console.WriteLine ( "Outer task beginning."); Task _child = Task.Factory.StartNew (() = {thread.spinwait (5000000); Console.WriteLine ( "Detached task completed."); }); _outer. Wait (); Console.WriteLine ( "Outer task completed."); catch (Exception ex) {Console.WriteLine (string. Format ( "Exception message:{0}", ex. Message.trim ())); } finally {console.readline ();}}}          

Code effects

5. Create Sub-tasks

Using System;Using System.Diagnostics;Using System.Threading;Using System.Threading.Tasks;Namespace consoleapplication17{Class Program {Staticvoid Main (String[] (args) {try {/** If you specify the attachedtoparent option when creating a task with user code running in a task,* The new task is called a subtask of the original task, and the original task is called the parent task. *  so you can use the AttachedToParent option represents a structured task parallelism. ———— msdn *   */Task _outer = Task.Factory.StartNew (() = {Console.WriteLine ( span class= "str" > "Outer task beginning."); Task _child = Task.Factory.StartNew (() = {thread.spinwait (5000000); Console.WriteLine ( "Detached task completed."), ); }); _outer. Wait (); Console.WriteLine ( "Outer task completed."); catch (Exception ex) {Console.WriteLine (string. Format ( "Exception message:{0}", ex. Message.trim ())); } finally {console.readline ();}}}          

Code effects

C # Task Parallel library using subtotal Z

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.