question

MarkusFreitag-0088 avatar image
0 Votes"
MarkusFreitag-0088 asked MarkusFreitag-0088 commented

Async - Task ; Thread

Hello!

Is there an overview of what should be taken when and, above all, how?
I am looking for a good overview with an explanation of why?

  • Thread

  • Task.Run

  • TaskFactory.Run

  • Await - Async Sample is good async

Thank you in advance for good examples and solutions.




When do I use a new function in Event, when do I use the lambda notation? Are there rules about when what and how?

   Serial.Receive += newFunction;
   Serial.Receive += (req) => {  make something   };


There are so many possibilities and variants that I no longer know what to take and when.

dotnet-csharp
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You asked 5 loosely related questions. A thread is a system resource and defined in the docs. Task.Run and TaskFactory.StartNew are methods that queue work on the thread pool. It seems you already have the docs for the async/await pattern?

Line 2 in the sample code is a way to create an anonymous method rather than a using a defined method. See the C# lambda documentation.


1 Vote 1 ·

You asked 5 loosely related questions.

Right, because I have a difficult time finding the right pattern.

Do you know typical examples, use cases?

You already have the docs for the async/await pattern?

What are the advantages and disadvantages compared to thread?

Task and thread are the same thing, aren't they?

0 Votes 0 ·
AgaveJoe avatar image AgaveJoe MarkusFreitag-0088 ·

Right, because I have a difficult time finding the right pattern. Do you know typical examples, use cases?

Your question covers definitions, patterns, and constructs. Is there some reason why you don't read the docs?

A thread is a system resource that executes code. The Thread class is used in the TPL pattern.

Use async/await for non CPU bound tasks; making an HTTP request or querying the database. Use Task.Run() for a CPU bound tasks. Do not mix the two because Task.Run() can block async/await. This concept is covered in the link you provided.

The code sample is two different ways to assign a logical block of code. The first uses a defined method the second uses a lambda; an anonymous method. The lambda benefit is you can see the code right where it is being called. It's just syntax. Please read the docs...


1 Vote 1 ·
Show more comments

@MarkusFreitag-0088
Some of the questions have been fully discussed on Stackoverflow, and I think their answers may be helpful to you.
Task vs Thread differences
What is the difference between Task.Run() and Task.Factory.StartNew()
Anonymous methods vs. lambda expression

1 Vote 1 ·

1 Answer

karenpayneoregon avatar image
1 Vote"
karenpayneoregon answered

See the following blog post and for even more see all of Steven's posts where there are more than you are after but easy to pick the right post out from the list.

Then there is an important repository on cancellations.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.