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:
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
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:
Call a method (with an argument) after a delay
(3 answers)
Fire method after variable amount of time
(2 answers)
How to call a method after so many seconds?
(5 answers)
Closed 5 years ago.
I'm trying to call an method in this if statement after a certain time (15 seconds) has ended, but it doesn't seem to work at all not print out the text in the method. How would I go about doing this, I have tried researching on how the stopwatch works but it doesn't seem to be working. Thank your for your help.
I have tried this code
Stopwatch watch = Stopwatch.StartNew();
if (watch.Elapsed.TotalMilliseconds == 15)
{
Method();
}
Task is what you need
Task.Delay(new TimeSpan(0, 0, 15)).ContinueWith(o => { Method(); });
More about Asynchronous programming here
What you are trying to do is to halt your execution for a short while, and then invoke the method.
The most trivial way to do so is:
Thread.Sleep(TimeSpan.FromSeconds(15));
Method();
But note that your thread is not doing anything during this time, so a better way would be to use the TPL, and async methods, but for your needs the code above will probably do :)