dotnet Core Asynchronous task

Source: Internet
Author: User
Tags dotnet

The thread pool provides a pool of background threads, manages threads on its own, and increases or decreases the number of threads in the thread pool as needed. Threads in the thread pool are used to perform some actions and still return to the thread pool.

Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading;using System.threading.tasks;using System.io;namespace tasksamples{class Program {static void Main () {tasksusing      ThreadPool ();    Console.ReadLine ();      } static void Tasksusingthreadpool () {var tf = new TaskFactory (); Task t1 = tf.      StartNew (Taskmethod, "Using a task factory");      Task t2 = Task.Factory.StartNew (Taskmethod, "Factory via a Task");      var t3 = new Task (Taskmethod, "using a Task constructor and Start"); T3.      Start ();    Task T4 = Task.run (() = Taskmethod ("Using the Run method");    } static Object Taskmethodlock = new Object ();        static void Taskmethod (object title) {Lock (Taskmethodlock) {Console.WriteLine (title); Console.WriteLine ("Task ID: {0}, Thread: {1}", Task.currentid = = null?)       "No Task": Task.CurrentId.ToString (), Thread.CurrentThread.ManagedThreadId); Console.WriteLine ("Is pooled thread: {0}", Thread.CurrentThread.IsThreadPoolThread);        Console.WriteLine ("Is background thread: {0}", Thread.CurrentThread.IsBackground);      Console.WriteLine (); }    }  }}

  

Instantiate the TaskFactory class, and pass the method that will be executed as a parameter to the StartNew method of the instance

var tf = new TaskFactory (); Task t1 = tf. StartNew (Taskmethod, "Using a task factory");

Use the static property of the task class TaskFactory to access TaskFactory and call its StartNew method

Task t2 = Task.Factory.StartNew (Taskmethod, "Factory via a Task");

Instantiate a task object using the constructor of the task class and call its Start method

var t3 = new Task (Taskmethod, "using a Task constructor and Start"); T3. Start ();

Call the static method of the task class run, the argument is an action delegate

Task T4 = Task.run (() = Taskmethod ("Using the Run method");

dotnet Core Asynchronous task

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.