如何忘记在C#异步方法同步上下文 [英] how to forget synchronization context in async methods in c#

查看:673
本文介绍了如何忘记在C#异步方法同步上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我想写一个异步方法M.我不知道什么样的同步上下文将被使用(UI,ASP.NET,控制台应用程序等)来调用它。我想使该方法尽可能容易使用。这意味着,任何人都应该能够通过访问返回任务的结果构件同步地调用它。

 公共异步任务< INT> M()
{
    调用的辅助方法,包括在等着,等//很多的
    //辅助方法不归我
    // ...    返回42;
}
//这个应该是安全的任何同步上下文做
INT结果= M()结果。 //同步等待

问题是子程序M的调用程序的同步环境泄漏到M的被叫方如果它们中的任何一个人想继续在同步上下文传递的,那么它可以死锁(使用UI同步时如上下文)。

的一个解决方案忘记当M被称为同步上下文是包装M的Task.Run的内容。然而,这将迫使一个线程跳转,其中包括在情况下,当完成内部m该任务和一切可能不同步上下文切换同一个线程上运行。有没有更好的解决办法忘记一个线程的同步上下文?还是我失去了一些东西,这将使这个问题无关紧要?


解决方案

  

我想使该方法一样容易使用的可能。这意味着,任何人都应该能够通过访问返回任务的结果构件同步地称它为


有没有简单的方法来创建一个同步封装对于异步方法。最好是让异步方法是异步的。

您的可以的使用 ConfigureAwait(continueOnCapturedContext:FALSE)丢弃的背景下,但它不会有任何好于 Task.Run 你的情况,因为你不能改变的 M 来电。

Let's say I want to write an async method M. I don't know what kind of synchronization context will be used (UI, ASP.NET, Console app, etc.) to call it. I'd like to make the method as easy to use as possible. That means that anyone should be able to call it synchronously by accessing the Result member of the returned Task.

public async Task<int> M()
{
    // lot's of calling of helper methods including awaits, etc.
    // helper methods not owned by me
    // ...

    return 42;
}


// this should be safe to do from any synchronization context
int result = M().Result; // Synchronously wait

The problem is that the synchronization context of the caller of method M is leaked to the callees of M. If any one of them wants to continue on the passed in synchronization context, then it can deadlock (e.g. when using the UI synchronization context).

One solution to forget the synchronization context when M is called is to wrap the content of M in Task.Run. However, that will force a thread jump, including in cases when all tasks inside M are complete and everything could synchronously run on the same thread without context switches. Is there a better solution to forget the synchronization context of a thread? Or am I missing something, which would make this question irrelevant?

解决方案

I'd like to make the method as easy to use as possible. That means that anyone should be able to call it synchronously by accessing the Result member of the returned Task.

There is no easy way to create a synchronous wrapper for an asynchronous method. It's best to let asynchronous methods be asynchronous.

You can use ConfigureAwait(continueOnCapturedContext: false) to discard the context, but it won't be any better than Task.Run in your case since you can't change the methods that M calls.

这篇关于如何忘记在C#异步方法同步上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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