一个接一个地在线性顺序中调用C#任务

时间:2015-02-24 04:27:51

标签: c# asp.net-mvc task asp.net-4.5

我需要一个接一个地调用多个任务,任务的结果应该依赖于另一个任务的开始。另外我们如何从任务方法返回失败的结果?由于我的任务方法在返回数据时具有逻辑,并且基于某些其他条件,它应该返回失败的结果,并且基于该下一个任务方法不应该执行。

这是我的控制器动作 -

        //
        // 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);
        }

以下是服务方法 -

        /// <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));
        }

我已经查看了这篇SO帖子,但在我的情景中无法使其正常工作 - Run sequence of tasks, one after the other

1 个答案:

答案 0 :(得分:-1)

您可以使用ContinueWith执行任务。这些可以链接。 https://msdn.microsoft.com/en-us/library/dd270696%28v=vs.110%29.aspx。例如:

 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++)
            { }
        }
    }