I am working on an ASP.NET MVC 4 web application. I am using .NET 4.5 and trying to use the new asynchronous APIs.
I have a couple of situations where I want to schedule the launch of async Task later, when I immediately return immediately the importance immediately. For example, here is the Login method that I want to return the new SessionID as soon as possible, but as soon as I get back to the SessionID, I want to clear the old SessionID:
public async Task<Guid> LogIn(string UserName, string Password) { //Asynchronously get ClientID from DB using UserName and Password Session NewSession = new Session() { ClientID = ClientID, TimeStamp = DateTime.Now }; DB.Sessions.Add(NewSession); await DB.SaveChangesAsync(); //NewSession.ID is autopopulated by DB CleanSessions(ClientID); //Async method which I want to execute later return NewSession.ID; } private async void CleanSessions(int ClientID) { //Asynchronously get expired sessions from DB based on ClientID and mark them for removal await DB.SaveChangesAsync(); }
I tried a bunch of different things, including combinations of Task.Run () and Parallel.Invoke (), but CleanSessions is never called. How to achieve planning of background tasks?
user1084447
source share