Novice talking about C#task asynchronous programming

Source: Internet
Author: User

Task is a powerful tool for Microsoft's new asynchronous programming in the. NET Framework 4.0, of course 4.5 new async, await, here we first say task-related.

In the actual programming, we use more is Task, Task.Factory.StarNew, Task.run, next simple expression under my understanding.

  

// incoming func delegate task<intnew task<intreturn (int11); Task . Start (); Console.WriteLine (Task. Result); // Incoming Action Delegate New Task (() = Console.WriteLine ("a")); Task1. Start ();

The instantiated task object needs to call start to start the task, and if you use Task.Factory.StarNew, you do not have to call the Start method to start the task.

var task = task.factory.startnew<intreturn1;}); Console.WriteLine (Task. Result); var task1 = Task.Factory.StartNew (() = Console.WriteLine (" this is StartNew ... "));

Task.run is not much different from Task.Factory.StarNew and new task, the difference is that the first two are put into the thread pool for immediate execution, while Task.run is executing when the thread pool is idle.

The Run method accepts only the non-parameter action and Func delegates, and the other two accept an argument of type object.

TaskFactory in MSDN. StartNew's note information is as follows:

They can either call the Wait method to block the current thread, or they can get the return value through Task.result, which, of course, blocks the current thread.

Then say the usual continuewith, this is a continuation of the execution of a task, similar to callback

var task = task.factory.startnew<intreturn1;}); Console.WriteLine (Task. Result); var result = task. continuewith<intreturn1;})); Console.WriteLine (Result. Result);

ContinueWith accepts an action or Func delegate, the first parameter of the delegate is a task type, which allows access to the previous Task object

Novice talking about C#task asynchronous programming

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.