Difference between Task and Thread C # [closed]

1

Someone would be kind enough to explain what is the difference between Task and Thread in C # and some of its methods, the information I find is usually somewhat ambiguous and confusing.

I want to implement a method that checks data from a REST API and that does it with an infinite loop with pauses and in another method that pauses about 2 seconds to go to the next line of code or call some method.

    
asked by 27.05.2018 в 06:00
source

1 answer

0

Thread is a concept of lower level of abstraction, generally it is no longer recommended to work directly with this class, although it allows you to manipulate important things as if the Thread runs in the background or foreground, it also allows you to use synchronization primitives among many other things.

Now Task and Task<T> are a pair of classes that bring a higher level of abstraction and are part of the TPL (Task Parallel Library) usually this is the class you want to use to make asynchronous code, since Task and Task they will make the best possible use of everything that .NET offers to make your code run asynchronously / parallel.

I would recommend that you study the TPL very well, studying it well you will realize how much work you save by working with Task instead of Thread

Review the following book .

EDITING

As you mention NaCI , asynchronous and parallel do not mean the same, clarifying the concepts:

Parallel processing: refers to distributing the work to be done in multiple threads that are working concurrently (at the same time)

Asynchronous processing: refers to the use of callbacks to minimize the use of new threads, that is, it uses the existing threads, in the case of C # the thread that initiates an asynchronous call is contained in a synchronization context that among other things has a reference to the thread to which the execution of the program has to return.

    
answered by 29.05.2018 в 19:09