简体   繁体   English

试图了解.net中的任务

[英]Trying to understand Tasks in .net

Ok I've been messing around with tasks in .net 4.5 and I'm trying to get my head around everything so I created a small app that does some work and I have a few questions about it. 好吧,我一直在乱搞.net 4.5中的任务,我正试图解决所有事情,所以我创建了一个小应用程序做了一些工作,我有几个问题。

Here is the code: 这是代码:

Account Controller.cs: Account Controller.cs:

namespace MvcApplication1.Controllers
{
    public class AccountController : Controller
    {
        private readonly UserManager _manager;

        public AccountController()
        {
            _manager = new UserManager(ContextFactory.GetContext());
        }

    [HttpPost]
    public async Task<ActionResult> Register(LoginModel model)
    {
        var user = await _manager.FindAsync(model.UserName);
        if (user != null)
        {
            //do some work
        }
        else
        {
            ModelState.AddModelError("", "Cannot Find User");
            return View(model);
        }
    }
  }
}

User Manager.cs: 用户管理器:

 namespace MvcApplication1
 {
   public class UserManager
   {
     private readonly IDBContext _context;

     public UserManager(IDBContext context)
     {
       _context = context;
     }
     public Task<UserProfile> FindAsync(string ID)
     {
       var queryHelper = new UserQueries(_context);
       var localID = ID;

        return Task.Factory.StartNew(() => queryHelper.GetProfile(localID));
      }
   }
 }

UserQueries.cs: UserQueries.cs:

public class UserQueries
{
    private readonly IDBContext _context;
    public UserQueries(IDBContext context)
    {
        _context = context;
    }

    public UserProfile GetProfile(string ID){
        DataTable dt = _context.ExecuteDataTable(...)
        //do work with dt and return UserProfile
    }
  }
}

IDBContext.cs IDBContext.cs

public interface IDBContext
{
    DataTable ExecuteDataTable(string sql, IEnumerable<SqlParameter> parameters);
}

So I have a few questions here. 所以我在这里有几个问题。

Do I have to create a new instance of the UserQuery class in every function? 我是否必须在每个函数中创建UserQuery类的新实例? Not sure if it needs to be sync locked or not, still confused there. 不确定是否需要同步锁定,仍然在那里混淆。

Do I need to copy the ID to localID, I'm worried about scoping and reference changes when the function is called, is that even something to worry about in any case? 我是否需要将ID复制到localID,在调用函数时我担心范围和引用更改,在任何情况下都要担心什么?

Should UserQueries be Task<UserProfile> as well? UserQueries也应该是Task<UserProfile>吗?

If UserQueries should be Task<UserProfile> should the DBContext that returns a DataTable also be Task<DataTable> , does .net have a function for Taskable sql calls? 如果UserQueries应该是Task<UserProfile> ,那么返回DataTable的DBContext也应该是Task<DataTable> ,.net是否有Taskable sql调用的函数?

I'm not 100% sure how deep you go with Task 我不是100%确定你使用Task有多深

unless ExecuteDataTable has a Async version built in I would recommend against using Tasks. 除非ExecuteDataTable内置了Async版本,否则我建议不要使用任务。 If you have to use Task.Factory.StartNew or Task.Run to make it work, you should likely not be doing it and you will get worse performance under load than you have gotten just leaving it synchronous. 如果你必须使用Task.Factory.StartNewTask.Run来使它工作,你可能不会这样做,你会在负载下获得比你让它同步更糟糕的性能。

Task.Run (and Task.Factory.StartNew ) should only be used when you are waiting on a task that is CPU bound . 只有在等待CPU绑定的任务时才应使用 Task.Run (和Task.Factory.StartNew )。 ExecuteDataTable is a I/O operation, you are waiting for the database to respond, whatever API you are using to talk to the database may expose functions that return Task and those you could await on (and you would need to await all the way up your chain of calls to the first call) but if it does not expose a function that returns a Task you should just do it the normal way. ExecuteDataTable是一个I / O操作,您正在等待数据库响应,无论您使用哪个API与数据库通信,都可能会暴露返回Task函数以及您可以等待的函数(并且您需要await一直向上)你的第一次调用的调用链)但是如果它没有公开一个返回Task的函数,你应该按照正常的方式去做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM