0

I'm working with AsyncController in .NET4.5 project. The controller is implemented using MVC 3 pattern where 2 methods are used in order to achieve asynchrony. Here is an example:

public class PortalController : AsyncController {
    public void NewsAsync(string city) {

        AsyncManager.OutstandingOperations.Increment();
        NewsService newsService = new NewsService();
        newsService.GetHeadlinesCompleted += (sender, e) =>
        {
            AsyncManager.Parameters["headlines"] = e.Value;
            AsyncManager.OutstandingOperations.Decrement();
        };
        newsService.GetHeadlinesAsync(city);
    }

    public ActionResult NewsCompleted(string[] headlines) {
        return View("News", new ViewStringModel
        {
            NewsHeadlines = headlines
        });
    }
}

In the above code I need to call:

private async Task<Foo> doWorkAsync(string bar) {
    // do work async
}

I can't refactor the existing code to make Controller return Task<ActionResult> at this time, as it looks like it will take a lot of time. So my question is what would be the best way to call an async method from within NewsAsync()?

foobar
  • 635
  • 12
  • 15
  • 1
    Possible duplicate of [How to call asynchronous method from synchronous method in C#?](http://stackoverflow.com/questions/9343594/how-to-call-asynchronous-method-from-synchronous-method-in-c) – Igor Apr 27 '16 at 10:54
  • never done this but try using `Task.Run(()=> asyncMethod())` – Spluf Apr 27 '16 at 11:36

0 Answers0