Awaiting tasks: Return task or await if no code after await
Date : March 29 2020, 07:55 AM
this one helps. await is used to continue execution of method body when awaiting completed. If there is nothing to continue, then you don't need awaiting. You can think of await (simplified) as of ContinueWith operation. So, your first method is something like: foreach (var something in someCollection)
DoSomething();
DoSomethingElse();
DoMethodAsync().ContinueWith(t => {});
|
Learning C#'s async/await/Task structure; code hangs at await?
Tag : chash , By : Dmitry
Date : March 29 2020, 07:55 AM
wish of those help You haven't actually run any Task. So you're waiting on something that will never complete. To fix your code exactly as it is, you can do this: private Foo RunExecute(out Task<Foo> task, bool async = false)
{
Foo outputFoo;
if(async)
{
task = Task.Run(() => makeFoo());
outputFoo = null;
}
else
{
task = null;
outputFoo = makeFoo();
}
return outputFoo;
}
private Foo RunExecute(out Task<Foo> task, bool async = false)
{
Foo outputFoo;
if(async)
{
task = makeFooAsync();
outputFoo = null;
}
else
{
task = null;
outputFoo = makeFoo();
}
return outputFoo;
}
async Task<Foo> makeFooAsync()
{
await Task.Delay(3000);
return new Foo();
}
private Task<Foo> RunExecute(bool async = false)
{
Foo outputFoo;
if(async)
{
return makeFooAsync();
}
else
{
return Task.FromResult(makeFoo());
}
}
|
Does the C# await can be compared to former old VB6 DoEvents?
Tag : chash , By : Joshua Johnson
Date : March 29 2020, 07:55 AM
around this issue In some ways they can appear similar at first glance. DoEvents will run a nested message loop to allow other methods to run on that thread before the current method finishes. Similarly, await will "yield" control to other methods, allowing other code to run on that thread before the current method finishes. However, there is one very, very important difference. DoEvents keeps the current call stack; when it invokes other methods, it does so directly; this causes serious problems with unexpected reentrancy, and is the primary reason for the oft-repeated phrase "DoEvents is evil". In contrast, await returns all the way up, so there's no direct reentrancy.
|
How to execute nested async/await code in parallel while maintaining the same thread on await continuations?
Tag : chash , By : user135518
Date : March 29 2020, 07:55 AM
wish of those help The methods that you are calling do not use ConfigureAwait(false). This means that we can force the continuations to resume in a context we like. Options: Install a single-threaded synchronization context. I believe Nito.Async has that. Use a custom TaskScheduler. await looks at TaskScheduler.Current and resumes at that scheduler if it is non-default. Task.Factory.StartNew(
() => MethodA()
, new ConcurrentExclusiveSchedulerPair().ExclusiveScheduler).Unwrap();
|
Async/ Await: why does the code that follows await is also executed on the background thread and not on the original pri
Tag : .net , By : obijywk
Date : March 29 2020, 07:55 AM
should help you out This is a result of application type you chose. Console apps and GUI apps behave differently regarding the SynchronizationContext. When you use await, then the current SynchronizationContext is captured and passed to the background thread. The idea is not to block the main thread by just waiting for the background thread to complete. The remaining code is enqueued and the current context stored in the SynchronizationContext which the backgroung thread wil capture. When the background thread completes, it returns the captured SynchronizationContext so that the enqueued remaining code can resume execution. You can get the current context by accessing SynchronizationContext.Current property. The code that is waiting for await to finish (the remaining code after await) will be enqueued as a continuation and executed on the captured SynchronizationContext.
|