C# 等待不释放异步方法中的调用线程

C# 等待不释放异步方法中的调用线程,c#,.net,task-parallel-library,async-await,C#,.net,Task Parallel Library,Async Await,我正在AsyncDemoViewModel中打印消息属性字符串列表 如果在getVideossLowyaSync()中使用Thread.Sleep(1000),则输出为 Time spent: 4001 milliseconds - Started on thread 18, finished on thread 18 - Started on thread 18, finished on thread 18 - Started on thread 18, finished o

我正在AsyncDemoViewModel中打印消息属性字符串列表

如果在getVideossLowyaSync()中使用Thread.Sleep(1000),则输出为

Time spent: 4001 milliseconds 

 - Started on thread 18, finished on thread 18  
 - Started on thread 18, finished on thread 18  
 - Started on thread 18, finished on thread 18 
 - Started on thread 18, finished on thread 18
Time spent: 4053 milliseconds 


 - Started on thread 12, finished on thread 19 
 - Started on thread 19, finished on thread 16 
 - Started on thread 16, finished on thread 19 
 - Started on thread 19, finished on thread 9
如果在GetVideosSlowyaSync()中使用wait Task.Delay(1000),则输出为

Time spent: 4001 milliseconds 

 - Started on thread 18, finished on thread 18  
 - Started on thread 18, finished on thread 18  
 - Started on thread 18, finished on thread 18 
 - Started on thread 18, finished on thread 18
Time spent: 4053 milliseconds 


 - Started on thread 12, finished on thread 19 
 - Started on thread 19, finished on thread 16 
 - Started on thread 16, finished on thread 19 
 - Started on thread 19, finished on thread 9
为什么等待调用没有释放调用线程?我希望等待版本的完成时间快4倍左右

控制器代码:

public class AsyncDemoController : Controller
{
    private DepartmentDb _db;

    public AsyncDemoController(DepartmentDb db)
    {
        _db = db; 
    }

    public async Task<ActionResult> Index()
    {

        var sw = Stopwatch.StartNew();
        var v1 = await GetVideosSlowlyAsync();
        var v2 = await GetVideosSlowlyAsync();
        var v3 = await GetVideosSlowlyAsync();
        var v4 = await GetVideosSlowlyAsync();

        var vm = new AsyncDemoViewModel() {Videos = v1.Item2, Messages = new List<string>()};
        sw.Stop();
        vm.Messages.Add(string.Format("Time spent: {0} milliseconds", sw.ElapsedMilliseconds));
        vm.Messages.Add(v1.Item1);
        vm.Messages.Add(v2.Item1);
        vm.Messages.Add(v3.Item1);
        vm.Messages.Add(v4.Item1);

        return View(vm);
    }

    private async Task<Tuple<string, IEnumerable<Video>>> GetVideosSlowlyAsync()
    {
        var t1 = Thread.CurrentThread.ManagedThreadId;
        await Task.Delay(1000); // Thread.Sleep(1000);
        var t2 = Thread.CurrentThread.ManagedThreadId;
        return Tuple.Create(string.Format("Started on thread {0}, finished on thread {1}", t1, t2), _db.Videos.AsEnumerable());
    }

}
公共类异步控制器:控制器
{
私人部门db_db;
公共异步数据控制器(部门数据库)
{
_db=db;
}
公共异步任务索引()
{
var sw=Stopwatch.StartNew();
var v1=等待GetVideossLowyaSync();
var v2=等待GetVideossLowyaSync();
var v3=等待GetVideossLowyaSync();
var v4=等待GetVideossLowyaSync();
var vm=new asynchdemoviewmodel(){Videos=v1.Item2,Messages=new List()};
sw.Stop();
Add(string.Format(“花费的时间:{0}毫秒”,sw.elapsedmillesons));
vm.Messages.Add(v1.Item1);
vm.Messages.Add(v2.Item1);
vm.Messages.Add(v3.Item1);
vm.Messages.Add(v4.Item1);
返回视图(vm);
}
专用异步任务GetVideossLowyaSync()
{
var t1=Thread.CurrentThread.ManagedThreadId;
等待任务。延迟(1000);//线程。睡眠(1000);
var t2=Thread.CurrentThread.ManagedThreadId;
返回Tuple.Create(string.Format(“在线程{0}上开始,在线程{1}上完成”,t1,t2),db.Videos.AsEnumerable());
}
}

您正在调用
等待GetVideosSlowyaSync()
,它将返回调用方法,直到任务完成。如果你想看到你期望的结果,你需要删除这个等待。这样,当
Task.Delay()
方法在线程池上休眠时,它将依次启动所有4个
getvideosslowyasync()

试试这样的

List<Task> taskList = new List<Task>();
taskList.Add(GetVideosSlowlyAsync());
taskList.Add(GetVideosSlowlyAsync());
taskList.Add(GetVideosSlowlyAsync());
taskList.Add(GetVideosSlowlyAsync());

Task.WaitAll(taskList.ToArray())
foreach (Task task in taskList)
    vm.Messages.Add(task.Result.Item1);
List taskList=new List();
添加(getvideosslowyasync());
添加(getvideosslowyasync());
添加(getvideosslowyasync());
添加(getvideosslowyasync());
Task.WaitAll(taskList.ToArray())
foreach(任务列表中的任务)
vm.Messages.Add(task.Result.Item1);

您正在调用
等待GetVideosSlowyaSync()
,它将返回调用方法,直到任务完成。如果你想看到你期望的结果,你需要删除这个等待。这样,当
Task.Delay()
方法在线程池上休眠时,它将依次启动所有4个
getvideosslowyasync()

试试这样的

List<Task> taskList = new List<Task>();
taskList.Add(GetVideosSlowlyAsync());
taskList.Add(GetVideosSlowlyAsync());
taskList.Add(GetVideosSlowlyAsync());
taskList.Add(GetVideosSlowlyAsync());

Task.WaitAll(taskList.ToArray())
foreach (Task task in taskList)
    vm.Messages.Add(task.Result.Item1);
List taskList=new List();
添加(getvideosslowyasync());
添加(getvideosslowyasync());
添加(getvideosslowyasync());
添加(getvideosslowyasync());
Task.WaitAll(taskList.ToArray())
foreach(任务列表中的任务)
vm.Messages.Add(task.Result.Item1);

等待方法等待GetVideosSlowyaSync方法完成。您需要将等待移动到需要操作结果的位置:

public async Task<ActionResult> Index()
{

    var sw = Stopwatch.StartNew();
    var v1 = GetVideosSlowlyAsync();
    var v2 = GetVideosSlowlyAsync();
    var v3 = GetVideosSlowlyAsync();
    var v4 = GetVideosSlowlyAsync();

    var vm = new AsyncDemoViewModel() {Videos = (await v1).Item2, Messages = new List<string>()};
    sw.Stop();
    vm.Messages.Add(string.Format("Time spent: {0} milliseconds", sw.ElapsedMilliseconds));
    vm.Messages.Add((await v1).Item1);
    vm.Messages.Add((await v2).Item1);
    vm.Messages.Add((await v3).Item1);
    vm.Messages.Add((await v4).Item1);

    return View(vm);
}
公共异步任务索引()
{
var sw=Stopwatch.StartNew();
var v1=getvideosslowyasync();
var v2=getvideosslowyasync();
var v3=getvideosslowyasync();
var v4=getvideosslowyasync();
var vm=new asynchdemoviewmodel(){Videos=(wait v1).Item2,Messages=new List()};
sw.Stop();
Add(string.Format(“花费的时间:{0}毫秒”,sw.elapsedmillesons));
添加((wait v1.Item1);
添加((wait v2.Item1);
添加((等待v3.Item1);
添加((等待v4.Item1);
返回视图(vm);
}

等待方法等待GetVideosSlowyaSync方法完成。您需要将等待移动到需要操作结果的位置:

public async Task<ActionResult> Index()
{

    var sw = Stopwatch.StartNew();
    var v1 = GetVideosSlowlyAsync();
    var v2 = GetVideosSlowlyAsync();
    var v3 = GetVideosSlowlyAsync();
    var v4 = GetVideosSlowlyAsync();

    var vm = new AsyncDemoViewModel() {Videos = (await v1).Item2, Messages = new List<string>()};
    sw.Stop();
    vm.Messages.Add(string.Format("Time spent: {0} milliseconds", sw.ElapsedMilliseconds));
    vm.Messages.Add((await v1).Item1);
    vm.Messages.Add((await v2).Item1);
    vm.Messages.Add((await v3).Item1);
    vm.Messages.Add((await v4).Item1);

    return View(vm);
}
公共异步任务索引()
{
var sw=Stopwatch.StartNew();
var v1=getvideosslowyasync();
var v2=getvideosslowyasync();
var v3=getvideosslowyasync();
var v4=getvideosslowyasync();
var vm=new asynchdemoviewmodel(){Videos=(wait v1).Item2,Messages=new List()};
sw.Stop();
Add(string.Format(“花费的时间:{0}毫秒”,sw.elapsedmillesons));
添加((wait v1.Item1);
添加((wait v2.Item1);
添加((等待v3.Item1);
添加((等待v4.Item1);
返回视图(vm);
}