This question already has answers here:
Task parallel library replacement for BackgroundWorker?
(2 answers)
Task cancellation with async task
(1 answer)
Task cancellation best practices
(1 answer)
Closed 2 years ago.
i try to understand the difference between Tasks and Backgroundworker respectively the using of it.
question 1: what is the difference? it is better management of system-resources? excepting the different syntax.
question2:
in a backgroundWorker i can wait inside for a event or its end before jump back (While (bw.isbusy && !bw.CancellationPending)). How to do this in a Task?
question 3:
BW.supportscancelation = true. How to do this in a Task?
I lack the perspective here ;-(
Thanks for your support!
Chris
Related
This question already has answers here:
async/await different thread ID
(4 answers)
async - stay on the current thread?
(3 answers)
Is it needed to make fields thread-safe when using async/await?
(4 answers)
Do async and await produce acquire and release semantics?
(1 answer)
Closed last year.
Given the following code:
public static async Task TestTask()
{
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
await Task.Delay(1000);
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
}
We see that the current thread is changed after the await. However, this code is not multi-threaded and instead it is meant to provide non-blocking IO (with reference to JavaScript's mechanism). But why does it happen? and since it happens, does it mean that anything that comes after the awaited line must consider thread-safety (for example adding to a HashSet) while that would not be needed before that awaited line?
This question already has answers here:
async/await. Where is continuation of awaitable part of method performed?
(3 answers)
How do yield and await implement flow of control in .NET?
(5 answers)
Brief explanation of Async/Await in .Net 4.5
(3 answers)
How does the async/await return callchain work?
(2 answers)
Closed 2 years ago.
Suppose I have async function with the following code.
var reply = await httpClient.GetAsync(somePath);
ProcessReply(reply);
The async function with this code is called in main thread of WPF application. When I execute it ProcessReply always runs asynchronously from dispatcher in main thread, for example if I block main thread continuation is not reached. What controls which thread runs the code after await? Is it guaranteed or possible to make it guaranteed that part after await runs in main thread?
This question already has answers here:
Difference between passing a regular and async anonymous function to Task.Run for asynchronous work
(2 answers)
What is the difference between Task.Run() and await Task.Run()?
(2 answers)
Closed 3 years ago.
I'm having a bit of a sanity check that needs resolving:
Is there a difference between
Task.Run(async () => await dothingAsync());
and
Task.Run(dothingAsync);
Edit (signature of dothingasync)
private Task dothingAsync();
Or
private async Task dothingAsync();
Here's a blog post on the subject that dives into more detail. There's also this Stack Overflow post that is very similar except it deals with methods and your question deals with lambdas (which are converted by the compiler into methods).
In summary, the two approaches are almost equivalent. Using the keywords does introduce a state machine. In this case - since the lambda just calls a single method and returns its result - eliding the keywords is appropriate. If you do include async and await, it would just add a bit of overhead for no benefit.
This question already has answers here:
How and when to use ‘async’ and ‘await’
(25 answers)
How does await async work in C# [closed]
(2 answers)
Closed 4 years ago.
I'm getting the basics down with async/await in C# and can't think of any reasons why someone is calling an async method that returns Task which they immediately await.
Would this not be better just to run the method synchronously to reduce the overhead of running the async method?
Or could it be the case of the method signature already being set that is dictating how it is being called?
This question already has answers here:
Alternative to Thread.Sleep in C#?
(10 answers)
async/await with thread timer
(2 answers)
Keeping UI responsive during a Thread.Sleep() [duplicate]
(3 answers)
Closed 5 years ago.
I'm porting a library to .NET core and to maximize portability I need to eliminate the dependency on System.Threading.Thread, and therefore Thread.Sleep. Whats an alternative to this?
you can use
Task.Delay(2000).Wait(); // Wait 2 seconds with blocking
await Task.Delay(2000); // Wait 2 seconds without blocking