我不能等待的异步任务没有使它异步作废? [英] Can I not await for async Task without making it async void?

查看:168
本文介绍了我不能等待的异步任务没有使它异步作废?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用异步任务的方法则需要方法之前放置等待。我需要code在非UI阻塞的方式来执行的,不想等待。我唯一​​的想法就是用:

 私人无效TaskFactory()
{
    CancellationTokenSource token_TaskFactory =新CancellationTokenSource();
    ParallelOptions parOpts =新ParallelOptions();
    parOpts.CancellationToken = token_TaskFactory.Token;
    parOpts.MaxDegreeOfParallelism = Environment.ProcessorCount;
    TaskCreationOptions ATP =新TaskCreationOptions();
    ATP = TaskCreationOptions preferFairness。
    任务TaskFactory = Task.Factory.StartNew(()=>
    {
       如果(!token_TaskFactory.IsCancellationRequested)
       {
         Thread.sleep代码(5000);
       }
       其他
       {       }
    },token_TaskFactory.Token,ATP,TaskScheduler.Default);
}


解决方案

  

在使用它需要方法之前放置等待异步任务的方法。


要正确的操作方法是,等待的方法,并调用方法异步任务异步任务< TResult> 。这将有异步 A级联效果通过您的code向上行进。

在一个用户界面应用程序,你通常会在一个事件处理程序结束,其中的不能的是异步任务,并在这一点上,你可以使事件处理程序异步无效

如果您通过一个普通的方法欺骗这个过程异步无效,您会导致其他问题,特别是围绕错误处理和可组合(即单元测试)。请参见我的MSDN文章或<一个href=\"http://channel9.msdn.com/Series/Three-Essential-Tips-for-Async/Tip-1-Async-void-is-for-top-level-event-handlers-only\">one许多谈判的关于为什么异步无效应尽量避免。


  

我需要code在非UI阻塞的方式来执行,不想等待。


为什么你不要用等待?你有异步code,你需要在一个非阻塞的方式运行,这是的到底是什么等待呢!

When using async Task method it is required to place await before method. I need code to be executed in non UI blocking manner and don't want to await. My only idea is to use:

private void TaskFactory()
{ 
    CancellationTokenSource token_TaskFactory = new CancellationTokenSource();
    ParallelOptions parOpts = new ParallelOptions();
    parOpts.CancellationToken = token_TaskFactory.Token;
    parOpts.MaxDegreeOfParallelism = Environment.ProcessorCount;
    TaskCreationOptions atp = new TaskCreationOptions();     
    atp = TaskCreationOptions.PreferFairness;
    Task TaskFactory = Task.Factory.StartNew(() => 
    {
       if (!token_TaskFactory.IsCancellationRequested)     
       {
         Thread.Sleep(5000);
       }
       else
       {

       }
    }, token_TaskFactory.Token, atp, TaskScheduler.Default); 
}

解决方案

When using async Task method it is required to place await before method.

The correct way to handle this is to await the method, and make the calling method async Task or async Task<TResult>. This will have a cascading effect as async travels up through your code.

In a UI application, you will usually end up at an event handler, which cannot be async Task, and at that point you can make the event handler async void.

If you cheat this process by making a regular method async void, you cause other problems, particularly around error handling and composability (i.e. unit testing). See my MSDN article or one of the many talks about why async void should be avoided.

I need code to be executed in non UI blocking manner and don't want to await.

Why don't you "want" to use await? You have asynchronous code that you need to run in a non-blocking fashion, and that's exactly what await does!

这篇关于我不能等待的异步任务没有使它异步作废?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆