0

I need to invoke multiple Tasks one after the other and the result of the task should be dependent for the other to begin. Also how can we return failed result from the task method? As my task method is having a logic when it will return data and based on some other condition it should return failed result and based on that the next task method should not execute.

Here is my controller action-

        //
        // POST: /Account/Register
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    FacebookUserID = null,
                    UserName = model.Email,
                    Name = model.Name,
                    Password = model.Password,
                    LastFailedAttempt = null,
                    FailedAttempts = 0,
                    Status = model.Status,
                    UserType = model.UserType,
                    SourceType = model.SourceType,
                    DateCreated = DateTime.Now,
                    DateModified = null,
                    DateConfirmed = null,
                    Email = model.Email
                };

                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    user = await _userService.FindByNameAsync(model.Email);
                    await SignInAsync(user, false);
                    return RedirectToAction("Index", "Home");
                }
                AddErrors(result);
            }
            return View(model);
        }

And below are the service methods-

        /// <summary>
        /// Checks and Creates the new Application User
        /// </summary>
        Task IUserStore<T, int>.CreateAsync(T model)
        {
            Task taskInvoke = Task.Factory.StartNew(() =>
            {
                var appUser = GetUserByEmail(model.Email);
                if (appUser == null)
                {
                    User user = new User();
                    user.FacebookUserID = model.FacebookUserID;
                    user.Name = model.Name;
                    user.Email = model.Email;
                    user.Password = model.Password;
                    user.LastFailedAttempt = null;
                    user.FailedAttempts = 0;
                    user.Status = model.Status;
                    user.UserType = model.UserType;
                    user.SourceType = model.SourceType;
                    user.DateCreated = DateTime.Now;
                    user.DateModified = null;
                    user.DateConfirmed = null;

                    _uow.UserRepository.Add(user);
                    _uow.Commit();
                    return true;
                }
                else
                {
                    return false;
                    //what should I return here if the user already exists?
                }
            });
            return taskInvoke;
        }

        /// <summary>
        /// Finds and Returns the Application User by email
        /// </summary>
        Task<T> IUserStore<T, int>.FindByNameAsync(string email)
        {
            Task<T> taskInvoke = Task<T>.Factory.StartNew(() =>
            {
                return (T)GetUserByEmail(email);
            });
            return taskInvoke;
        }

        /// <summary>
        /// Finds and returns the user by email
        /// </summary>
        public ApplicationUser GetUserByEmail(string email)
        {
            return Mapper.DynamicMap<ApplicationUser>(_uow.UserRepository.Get(x => x.Email == email));
        }

I had reviewed this SO post but wasn't able to get this working in my scenario- Run sequence of tasks, one after the other

Community
  • 1
  • 1
Manik Arora
  • 4,702
  • 1
  • 25
  • 48
  • Why are you using `tasks` then? – Hari Prasad Feb 24 '15 at 04:31
  • 1
    Please be more specific than "wasn't able to get this working". The answer in the other post seems relevant; why is it not in your case? – Peter Duniho Feb 24 '15 at 04:31
  • I have implemented the Simple Custom User Store for ASP.Net EF Identity 2.0 and need to override the Task methods of the IUserStore, IUserPasswordStore and IUserSecurityStampStore Interfaces – Manik Arora Feb 24 '15 at 04:34
  • @PeterDuniho the trouble is I cannot change the service method headers as those are required to be defined in the service class implementing the Interfaces. So something needs to be managed in Controller-Action only but can't get this through, any guidance would be highly appreciated – Manik Arora Feb 24 '15 at 05:22
  • 2
    Can't get what through? What exactly isn't working? – Yuval Itzchakov Feb 24 '15 at 05:32

1 Answers1

-1

You can use the ContinueWith for task execution. These can be chained. https://msdn.microsoft.com/en-us/library/dd270696%28v=vs.110%29.aspx. For example:

 class Program
    {
        static void Main(string[] args)
        {

            Task t1 = new Task(() => TaskOneWork());
            Task t2 = t1.ContinueWith((t) => TaskTwoWork());
            t1.Start();

            t2.Wait();
        }

        private static void TaskOneWork()
        {
            for(int i = 0 ; i < 1000000; i++)
            { }
        }

        private static void TaskTwoWork()
        {
            for (int i = 0; i < 1000000; i++)
            { }
        }
    }
Noel
  • 567
  • 1
  • 4
  • 11
  • The OP has indicated (granted, in a vague, opaque way) that `ContinueWith()` doesn't work in his scenario. Please explain how in spite of that, your example provides an answer to his question. – Peter Duniho Feb 24 '15 at 05:41