依次调用线性顺序的C#任务
问题描述:
我需要依次调用多个任务,并且任务的结果应该依赖于另一个开始。另外我们怎样才能从任务方法中返回失败的结果?由于我的任务方法在返回数据并基于其他条件时具有逻辑,所以它应该返回失败的结果,并基于该方法不应执行下一个任务方法。依次调用线性顺序的C#任务
这里是我的控制器行动 -
//
// 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后,但没能在我scenario-得到这个工作Run sequence of tasks, one after the other
答
您可以使用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++)
{ }
}
}
+0
OP已经表明(以模糊不清的方式授予)“ContinueWith()”在他的场景中不起作用。请解释一下,尽管如此,你的例子为他的问题提供了一个答案。 – 2015-02-24 05:41:48
为什么你使用'tasks'呢? – 2015-02-24 04:31:50
请比“无法得到这个工作”更具体。另一篇文章的答案似乎相关;为什么它不是你的情况? – 2015-02-24 04:31:58
我已经为ASP.Net EF Identity 2.0实现了简单的自定义用户存储,并且需要覆盖IUserStore,IUserPasswordStore和IUserSecurityStampStore接口的任务方法 – 2015-02-24 04:34:16