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