Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Task in C #

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to understand Task in C#". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The difference between Thread and Task

Tasks can return results, and there is no direct mechanism to return results from threads. The task supports cancellation by using a cancellation token, but the thread does not. A task can execute multiple threads at the same time, and threads can only run one task at a time. Asynchronism can be easily achieved using the async and await keywords. The new Thread does not handle thread pool threads, and Task does use thread pool threads. Tasks are concepts at a higher level than threads.

Task

The .NET Framework provides Threading.Tasks classes, and we can create tasks and run them asynchronously. Tasks are objects that represent some of the work that should be done. The task can tell us whether the work is completed, and the operation returns the result, then the task will provide us with the result.

Think deeply about Task

Thread is based on operating system-level threads, while ThreadPool and Task do not create their own operating system threads, which are executed by the Task Scheduler (TaskScheduler). The default scheduler runs only on ThreadPool. Unlike ThreadPool, Task can return the completion result at a specified time, and can also extend the task through ContinueWith to run more operations after the task has been executed, and callback immediately if it has been completed. You can also call Wait to synchronously wait for the task to finish, blocking thread execution like Thread.Join until the task is completed

Here comes the point.

Because tasks run on ThreadPool, creating default Task with no options is not suitable for performing long operations, as they may fill the system default thread pool with threads blocking other operations, but Task provides the LongRunning option, which tells the task scheduler (TaskScheduler) to start new threads instead of running on ThreadPool

Var task = Task.Factory.StartNew (() = > ("Hello World"), TaskCreationOptions.LongRunning)

Console.WriteLine (task.Result)

One of the main differences between tasks and threads is the propagation of exceptions. When using threads, if we get an exception in a long-running method, it is impossible to catch the exception in the parent function, but if we are using a task, it is easy to catch the exception.

This is the end of the content of "how to understand Task in C#". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report