Linked Questions
161 questions linked to/from How to call asynchronous method from synchronous method in C#?
42
votes
2
answers
44k
views
Is it possible to call an awaitable method in a non async method? [duplicate]
In a windows 8 application in C#/XAML, I sometimes want to call an awaitable method from a non asynchronous method.
Actually is it correct to replace this :
public async Task<string> ...
33
votes
3
answers
50k
views
"await" doesn't wait for the completion of call [duplicate]
I'm building a Metro App.
In the MainPage.xaml.cs, I instantiate Album as follows:
Album album = new Album(2012); //With the album ID as its parameter.
ListView1.ItemsSource = album.Songs;
In the ...
25
votes
2
answers
55k
views
How Do I Call an Async Method from a Non-Async Method? [duplicate]
I have the below method:
public string RetrieveHolidayDatesFromSource() {
var result = this.RetrieveHolidayDatesFromSourceAsync();
/** Do stuff **/
var returnedResult = ...
12
votes
1
answer
38k
views
What is the best practice to call Async method from Sync method? [duplicate]
I know some people will argue "why don't you just make SyncMethod() to async method?". We wish, but in a real world, sometimes we have to keep SyncMethod the way it is for backwards ...
1
vote
3
answers
5k
views
What is the correct way to call an async method from synchronous code, and block until the task is complete? [duplicate]
Consider this code:
public async Task DoStuffAsync() {
await WhateverAsync(); // Stuff that might take a while
}
// Elsewhere in my project...
public async Task MyMethodAsync() {
await ...
5
votes
1
answer
1k
views
Is there a way in C# to make a call to an async method without making the caller also async? [duplicate]
To be clearer, I am referencing a factory method that did not used to be async. In a recent API upgrade, they made the old method obsolete and created a new async method. Now, our code base is ...
0
votes
1
answer
2k
views
How we can return Task<HttpResponseMessage> return type to HttpResponseMessage type? [duplicate]
Is there a way where we can return TaskHttpResponseMessage type to HttpResponseMessage type?Below is the code.
private HttpResponseMessage Process(string receiver,HttpRequestMessage request)
...
2
votes
1
answer
948
views
How do I start in C# a function that's async from a function that isn't? [duplicate]
I have a function that isn't async and as a result I can't use await. In that function I call a function that's async.
My coworker suggested I use:
private void Foo()
{
var _ = BarAsync();
}
This ...
0
votes
0
answers
885
views
Call async task method from another class using wpf/C# [duplicate]
I'm trying to call an async Task method, from another class. The method is place within a service I have created.
Service:
public class GetRoomsService
{
public async Task<...
0
votes
0
answers
321
views
How To Use Async With A Non-Async Base Class - C#? [duplicate]
I'm building an app using an existing codebase that is not async-based. I'm wondering if it's practical to reuse this code or if I need to create async versions of the existing codebase. Here is an ...
0
votes
1
answer
145
views
How can I use async method inside sync method [duplicate]
I have a sync method as below:
public ActionResult Insert([FromBody] Model model) { }
and I also have an async method:
_myMethodAsync();
My async method returns int.
If I do like this I have to edit ...
1
vote
0
answers
146
views
how to call an async method synchronously? [duplicate]
All the 3rd partly API calls now use Async methods. I do not want to use them async. I want to convert them to syncronous because I do not care about a few seconds, and I can not seem to do async all ...
0
votes
0
answers
114
views
How to run async tasks synchronously (best practices)? [duplicate]
I've read so many posts on this, some of which are conflicting (possibly due to the age of the posts), so I just want to confirm my understanding of "best practices" as of .NET 4.8.
For an async ...
1
vote
0
answers
92
views
Make a sync method from async method [duplicate]
Suppose I had an async method like
public async Task<bool> GetBoolAsync()
{
// Do some stuff
return await GetMyBoolFromSomeFunction(somestuff);
}
and I wanted a synchronous version, ...
0
votes
0
answers
49
views
Await for a task inside an async method [duplicate]
I'm working with async-await methods and I can't get next functionality:
I have a sync wrapper method (MethodSync) that calls an async method (MethodA_Async) and then does some stuff (MethodB).
...