Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7518779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:47:54+00:00 2026-05-30T01:47:54+00:00

I have a public async Task Foo() method that I want to call from

  • 0

I have a public async Task Foo() method that I want to call from a synchronous method. So far all I have seen from MSDN documentation is calling async methods via async methods, but my whole program is not built with async methods.

Is this even possible?

Here’s one example of calling these methods from an asynchronous method:
Walkthrough: Accessing the Web by Using Async and Await (C# and Visual Basic)

Now I’m looking into calling these async methods from synchronous methods.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:47:55+00:00Added an answer on May 30, 2026 at 1:47 am

    Asynchronous programming does "grow" through the code base. It has been compared to a zombie virus. The best solution is to allow it to grow, but sometimes that’s not possible.

    I have written a few types in my Nito.AsyncEx library for dealing with a partially-asynchronous code base. There’s no solution that works in every situation, though.

    Solution A

    If you have a simple asynchronous method that doesn’t need to synchronize back to its context, then you can use Task.WaitAndUnwrapException:

    var task = MyAsyncMethod();
    var result = task.WaitAndUnwrapException();
    

    You do not want to use Task.Wait or Task.Result because they wrap exceptions in AggregateException.

    This solution is only appropriate if MyAsyncMethod does not synchronize back to its context. In other words, every await in MyAsyncMethod should end with ConfigureAwait(false). This means it can’t update any UI elements or access the ASP.NET request context.

    Solution B

    If MyAsyncMethod does need to synchronize back to its context, then you may be able to use AsyncContext.RunTask to provide a nested context:

    var result = AsyncContext.RunTask(MyAsyncMethod).Result;
    

    *Update 4/14/2014: In more recent versions of the library the API is as follows:

    var result = AsyncContext.Run(MyAsyncMethod);
    

    (It’s OK to use Task.Result in this example because RunTask will propagate Task exceptions).

    The reason you may need AsyncContext.RunTask instead of Task.WaitAndUnwrapException is because of a rather subtle deadlock possibility that happens on WinForms/WPF/SL/ASP.NET:

    1. A synchronous method calls an async method, obtaining a Task.
    2. The synchronous method does a blocking wait on the Task.
    3. The async method uses await without ConfigureAwait.
    4. The Task cannot complete in this situation because it only completes when the async method is finished; the async method cannot complete because it is attempting to schedule its continuation to the SynchronizationContext, and WinForms/WPF/SL/ASP.NET will not allow the continuation to run because the synchronous method is already running in that context.

    This is one reason why it’s a good idea to use ConfigureAwait(false) within every async method as much as possible.

    Solution C

    AsyncContext.RunTask won’t work in every scenario. For example, if the async method awaits something that requires a UI event to complete, then you’ll deadlock even with the nested context. In that case, you could start the async method on the thread pool:

    var task = Task.Run(async () => await MyAsyncMethod());
    var result = task.WaitAndUnwrapException();
    

    However, this solution requires a MyAsyncMethod that will work in the thread pool context. So it can’t update UI elements or access the ASP.NET request context. And in that case, you may as well add ConfigureAwait(false) to its await statements, and use solution A.

    Update: 2015 MSDN article ‘Async Programming – Brownfield Async Development‘ by Stephen Cleary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to call a Async Task with listener from the TimerTask eack 1.5
I have Async Task that must write folder Files from assets to folder on
I have method that does asynchronous call to web service. Something like that: public
I have this sample code for async operations (copied from the interwebs) public class
I have created an async task and want to change the message of progress
I have generic async task class which fetches response from server . And i
I have a TabActivity with 3 tabs. There is an async task that when
I have a public class FriendMaps extends MapActivity that gets called from a menu
I have a async task called within an activity: public class DownloadFile extends AsyncTask<String,
I have public and private keys in separate .pem files that I would need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.