-3

I currently have code that looks like this:

private async Task<ListFolderResult> ListFolder(DropboxClient client, string path) { Console.WriteLine("--- Files ---"); var list = await client.Files.ListFolderAsync(path); return list; } 

And is called like this:

var list = await ListFolder(client, path); 

I would like to change that code so the call looks like this:

var list = ListFolder(client, path); 

And the waiting moves to inside of the ListFolder method.

4
  • Just remove async/await from ListFolder and add waiting: client.Files.ListFolderAsync(path).Result or client.Files.ListFolderAsync(path).Wait() Commented Sep 23, 2017 at 22:56
  • if you want to post that as an answer as a complete code I will accept it and delete my answer. Commented Sep 23, 2017 at 22:57
  • perhaps the down voters could be bothered to post a comment as to why this is a bad question? Commented Sep 23, 2017 at 22:59
  • If duplicate I selected is not enough - make sure to check similar answers - bing.com/search?q=c%23+call+async+method+synchronously Commented Sep 24, 2017 at 8:03

1 Answer 1

-2

This is working for me in a console app, but I will be very happy to delete this answer if someone posts a more general answer.

private ListFolderResult ListFolder(DropboxClient client, string path) { Console.WriteLine("--- Files ---"); var list = client.Files.ListFolderAsync(path); list.Wait(); return list.Result; } 
Sign up to request clarification or add additional context in comments.

4 Comments

if there is something wrong with this answer then please comment or post an another answer.
Suggesting code that almost guaranteed to deadlock in most cases (except console or unit test) without good warning makes this suggestion bad answer. (ignoring fact that there are tons of better answers how to call async code synchronously)
BTW: return client.Files.ListFolderAsync(path).Result is enough. No need for Wait. Since now it is one line function, no need it to be a separate function
@Eser - post that as an answer and I will accept it and delete my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.