Why:threadpool has no built-in mechanism to mark when the current thread is finished, and there is no mechanism to get the return value when the operation is complete, thus launching a task to manage asynchronous threads more precisely.
How: Control how to create a task by constructing the parameter taskcreationoptions of the method to see the enumeration value of the enumeration. The result of getting a task execution has the Task.result property, which calls Wait () internally, such as WaitAny (), WaitAll (), and so on.
How do I cancel a task in execution?
Instantiate the Cancelletiontokensource class, pass the object as a parameter to the Async method, call the object's throwifcancellationrequested () in the Async method, and if the Cancel () method of the object is called, Then the Async method throws an exception operationcanceledexception, and in the main thread is captured with Try-catch (aggregateexception), the exception can be caught and processed.
Task_ canceling asynchronous compute throttling operations CancellationTokenSource and catching exceptions in asynchronous threads