I have read a lot of articles and still cant get understand this part.
Consider this code :
private async void button1_Click(object sender, EventArgs e)
{
await Dosomething();
}
private async Task<string> Dosomething()
{
await Task.Run((() => "Do Work"));
return "I am done";
}
First question:
When I click the button, it will Call DoSomething and await a Task that creates a Thread from the threadpool by calling Task.Run ( if I am not mistaken ) and all of this runs asynchronously. So I achieved creating a thread that does my work but doing it asynchronously? But consider that I don't need any result back, i just want the work to be done without getting any result back, is there really a need to use async/await , and if so, how?
Second question:
When running a thread asynchronously, how does that work? Is it running on the main UI but on a separate thread or is it running on a separate thread and separate is asynchronously inside that method?
The async and await keywords don't cause additional threads to be created. Async methods don't require multithreading because an async method doesn't run on its own thread. The method runs on the current synchronization context and uses time on the thread only when the method is active.
Note: Just using a Task in . NET code does not mean there are separate new threads involved. Generally when using Task. Run() or similar constructs, a task runs on a separate thread (mostly a managed thread-pool one), managed by the .
NET, Task. Run is used to asynchronously execute CPU-bound code.
Async/await helps you write synchronous-looking JavaScript code that works asynchronously. Await is in an async function to ensure that all promises that are returned in the function are synchronized. With async/await, there's no use of callbacks.
Simplest way to do a fire and forget method in C#?
There's a big difference if you don't await
the Task
or you await
it:
Case you don't await
it: DoSomething
is called but next sentence is executed while DoSomething
Task
hasn't been completed.
Case you await
it: DoSomething
is called and next sentence is executed once DoSomething
Task
has been completed.
So, the need of async
/await
will depend on how you want to call DoSomething
: if you don't await
it is like calling it the fire & forget way.
Is it running on the main UI but on a separate thread or is it running on a seperate thread and separate is asynchronously inside that method?
Asynchronous code sometimes means other thread (see this Q&A Asynchronous vs Multithreading - Is there a difference?). That is, either if the code is being executed in a separate thread from the UI one or it lets continue the processing of the UI thread while it gets resumed, it's nice because UI loop can still update the screen while other tasks are being done in parallel without freezing the UI.
An asynchronous method (i.e. async
method) is a syntactic sugar to tell the compiler that await
statements should be treated as a state machine. The C# compiler turns your async
/await
code into a state machine where code awaiting a Task
result is executed after the code that's being awaited.
You might want to review these other Q&As:
[...] But does this mean that "async/await" will fire off a thread and Task.Run also fires off a thread or are they both the same thread?
Using async
-await
doesn't mean "I create a thread". It's just a syntactic sugar to implement continuations in an elegant way. A Task may or may not be a thread. For example, Task.FromResult(true)
creates a fake task to be able to implement an async method without requirement it to create a thread:
public Task<bool> SomeAsync() { // This way, this method either decides if its code is asynchronous or // synchronous, but the caller can await it anyway! return Task.FromResult(true); }
The type Task<TResult>
requires you to return a TResult
from your task. If you don't have anything to return, you can use Task
instead (which, incidentally, is the base class of Task<TResult>
).
But keep in mind that a task is not a thread. A task is a job to be done, while a thread is a worker. As your program runs, jobs and workers become available and unavailable. Behind the scenes, the library will assign your jobs to available workers and, because creating new workers is a costly operation, it will typically prefer to reuse the existing ones, through a thread pool.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With