0

Ok i have been though many topics on this site and this whole async thing still confuses me so im going to keep it simple.

I Have a ActionMethod on a controller.

public async Task<ActionResult> Index()
    {

        try
        {
            throw new Exception("test");
        }
        catch (Exception ex)
        {
            new MD_Log().LogError();
        }

        return View();
    }

My ErrorLog function is async itself

 // adds a new error log
    public async Task LogError()
    {
        Thread.Sleep(10000);

        // add a record to the error table
        using (DB db = new DB())
        {
            // SomeCode
            await db.SaveChangesAsync();
        }
    }

I throw a new exception on my controller, the LogError() method gets called and i pause the execution of the log function for 10 seconds.

I do not want to wait for the LogError() function to complete because my users do not need to wait for that, so i leave out the await keyword from the new MD_Log().LogError() function within my controller action.

Now if i add a break point on the return View(); should that break point not be hit first before the await db.SaveChangesAsync() gets hit?

I am not adding the await keyword and i am adding a 10 second pause to the LogError function.

Whats going on here? this seems synchronous to me. How could i return to view while the LogError() function is still running in the background?

Kenny
  • 1
  • 2
  • 1
    No. When you throw an exception, it never GETS to the return view. It unwinds the call stack to the previous call, and if there is no catch there, it proceeds up the stack until it is caught by the runtime. In the case of ASP.NET, it caches the exception eventually and calls the LogError method, – Erik Funkenbusch Jul 18 '17 at 22:18
  • Ok you read way to much into that, i was using the throw new exception as an example. may be it was a bad example snippet of code to use. – Kenny Jul 19 '17 at 20:50
  • Then i'm not really sure what your real question is. – Erik Funkenbusch Jul 19 '17 at 21:06

0 Answers0