ChatGPT解决这个技术问题 Extra ChatGPT

如何编写带 out 参数的异步方法?

我想编写一个带有 out 参数的异步方法,如下所示:

public async void Method1()
{
    int op;
    int result = await GetDataTaskAsync(out op);
}

我如何在 GetDataTaskAsync 中执行此操作?


d
dcastro

您不能使用带有 refout 参数的异步方法。

Lucian Wischik 解释了为什么在这个 MSDN 线程上这是不可能的:http://social.msdn.microsoft.com/Forums/en-US/d2f48a52-e35a-4948-844d-828a1a6deb74/why-async-methods-cannot-have-ref-or-out-parameters

至于为什么 async 方法不支持 out-by-reference 参数? (或 ref 参数?)这是 CLR 的限制。我们选择以与迭代器方法类似的方式实现异步方法——即通过编译器将方法转换为状态机对象。 CLR 没有安全的方法将“输出参数”或“引用参数”的地址存储为对象的字段。支持 out-by-reference 参数的唯一方法是异步功能由低级 CLR 重写而不是编译器重写完成。我们研究了这种方法,它有很多用途,但最终成本会如此之高,以至于它永远不会发生。

这种情况的典型解决方法是让异步方法返回一个元组。您可以这样重写您的方法:

public async Task Method1()
{
    var tuple = await GetDataTaskAsync();
    int op = tuple.Item1;
    int result = tuple.Item2;
}

public async Task<Tuple<int, int>> GetDataTaskAsync()
{
    //...
    return new Tuple<int, int>(1, 2);
}

远非太复杂,这可能会产生太多问题。 Jon Skeet 在这里解释得很好stackoverflow.com/questions/20868103/…
感谢您提供 Tuple 替代方案。非常有帮助。
Tuple 很难看。 :P
我认为 C# 7 中的 Named Tuples 将是解决此问题的完美解决方案。
@orad 我特别喜欢这个: private async Task<(bool success, Job job, string message)> TryGetJobAsync(...)
j
jv_

The C#7+ Solution 是使用隐式元组语法。

    private async Task<(bool IsSuccess, IActionResult Result)> TryLogin(OpenIdConnectRequest request)
    { 
        return (true, BadRequest(new OpenIdErrorResponse
        {
            Error = OpenIdConnectConstants.Errors.AccessDenied,
            ErrorDescription = "Access token provided is not valid."
        }));
    }

返回结果使用方法签名定义的属性名称。例如:

var foo = await TryLogin(request);
if (foo.IsSuccess)
     return foo.Result;

我要补充一点,我刚刚开始在我的一个应用程序中大量使用它。结果模式是金色的。
s
superjos

async 方法中不能有 refout 参数(如前所述)。

这需要在移动的数据中进行一些建模:

public class Data
{
    public int Op {get; set;}
    public int Result {get; set;}
}

public async void Method1()
{
    Data data = await GetDataTaskAsync();
    // use data.Op and data.Result from here on
}

public async Task<Data> GetDataTaskAsync()
{
    var returnValue = new Data();
    // Fill up returnValue
    return returnValue;
}

您可以获得更轻松地重用代码的能力,而且它比变量或元组更具可读性。


我更喜欢这个解决方案而不是使用元组。更干净!
M
Michael Gehling

我遇到了同样的问题,因为我喜欢使用 Try-method-pattern,它基本上似乎与 async-await-paradigm 不兼容......

对我来说重要的是,我可以在单个 if 子句中调用 Try 方法,而不必预先定义外变量,但可以像以下示例中那样内联:

if (TryReceive(out string msg))
{
    // use msg
}

所以我想出了以下解决方案:

定义一个辅助结构: public struct AsyncOut { private readonly T returnValue;私有只读 OUT 结果; public AsyncOut(T returnValue, OUT result) { this.returnValue = returnValue; this.result = 结果; } public T Out(out OUT 结果) { result = this.result;返回返回值; } 公共 T ReturnValue => returnValue;公共静态隐式运算符 AsyncOut((T returnValue ,OUT result) tuple) => new AsyncOut(tuple.returnValue, tuple.result);像这样定义异步尝试方法: public async Task> TryReceiveAsync() { string message;布尔成功; // ... 返回(成功,消息);像这样调用异步 Try 方法: if ((await TryReceiveAsync()).Out(out string msg)) { // 使用 msg }

对于多个输出参数,您可以定义其他结构(例如 AsyncOut)或者您可以返回一个元组。


这是一个非常聪明的解决方案!
这怎么不是最佳答案之一,这个解决方案提供了与非异步版本的奇偶校验,不像其他让你返回一个元组然后运行你的 if 块的 anwsers。
S
Scott Turner

Alex 在可读性方面提出了一个很好的观点。等效地,一个函数也足以定义返回的类型,并且您还可以获得有意义的变量名称。

delegate void OpDelegate(int op);
Task<bool> GetDataTaskAsync(OpDelegate callback)
{
    bool canGetData = true;
    if (canGetData) callback(5);
    return Task.FromResult(canGetData);
}

调用者提供 lambda(或命名函数),智能感知通过从委托中复制变量名称来提供帮助。

int myOp;
bool result = await GetDataTaskAsync(op => myOp = op);

这种特殊方法类似于“Try”方法,如果方法结果为 true,则设置 myOp。否则,您不关心 myOp


C
Community

out 参数的一个很好的特性是,即使函数抛出异常,它们也可用于返回数据。我认为与使用 async 方法最接近的等效方法是使用新对象来保存 async 方法和调用者都可以引用的数据。另一种方法是pass a delegate as suggested in another answer

请注意,这两种技术都不会像 out 那样从编译器中强制执行。即,编译器不会要求您在共享对象上设置值或调用传入的委托。

下面是一个示例实现,它使用共享对象来模仿 refout,以便与 async 方法和 refout 不可用的其他各种场景一起使用:

class Ref<T>
{
    // Field rather than a property to support passing to functions
    // accepting `ref T` or `out T`.
    public T Value;
}

async Task OperationExampleAsync(Ref<int> successfulLoopsRef)
{
    var things = new[] { 0, 1, 2, };
    var i = 0;
    while (true)
    {
        // Fourth iteration will throw an exception, but we will still have
        // communicated data back to the caller via successfulLoopsRef.
        things[i] += i;
        successfulLoopsRef.Value++;
        i++;
    }
}

async Task UsageExample()
{
    var successCounterRef = new Ref<int>();
    // Note that it does not make sense to access successCounterRef
    // until OperationExampleAsync completes (either fails or succeeds)
    // because there’s no synchronization. Here, I think of passing
    // the variable as “temporarily giving ownership” of the referenced
    // object to OperationExampleAsync. Deciding on conventions is up to
    // you and belongs in documentation ^^.
    try
    {
        await OperationExampleAsync(successCounterRef);
    }
    finally
    {
        Console.WriteLine($"Had {successCounterRef.Value} successful loops.");
    }
}

J
Jerry Nixon

我喜欢 Try 模式。这是一个整齐的图案。

if (double.TryParse(name, out var result))
{
    // handle success
}
else
{
    // handle error
}

但是,async 具有挑战性。这并不意味着我们没有真正的选择。以下是您可以为 Try 模式的准版本中的 async 方法考虑的三种核心方法。

方法 1 - 输出结构

这看起来很像一个同步 Try 方法,它只返回一个 tuple 而不是带有 out 参数的 bool,我们都知道在 C# 中是不允许的。

var result = await DoAsync(name);
if (result.Success)
{
    // handle success
}
else
{
    // handle error
}

使用返回 falsetrue 并且从不抛出 exception 的方法。

请记住,在 Try 方法中抛出异常会破坏模式的整个目的。

async Task<(bool Success, StorageFile File, Exception exception)> DoAsync(string fileName)
{
    try
    {
        var folder = ApplicationData.Current.LocalCacheFolder;
        return (true, await folder.GetFileAsync(fileName), null);
    }
    catch (Exception exception)
    {
        return (false, null, exception);
    }
}

方法 2 - 传入回调方法

我们可以使用 anonymous 方法来设置外部变量。这是聪明的语法,虽然有点复杂。小剂量,没问题。

var file = default(StorageFile);
var exception = default(Exception);
if (await DoAsync(name, x => file = x, x => exception = x))
{
    // handle success
}
else
{
    // handle failure
}

该方法遵循 Try 模式的基础,但将 out 参数设置为传入回调方法。它是这样完成的。

async Task<bool> DoAsync(string fileName, Action<StorageFile> file, Action<Exception> error)
{
    try
    {
        var folder = ApplicationData.Current.LocalCacheFolder;
        file?.Invoke(await folder.GetFileAsync(fileName));
        return true;
    }
    catch (Exception exception)
    {
        error?.Invoke(exception);
        return false;
    }
}

我心中有个关于性能的问题。但是,C# 编译器非常聪明,我认为你可以安全地选择这个选项,几乎可以肯定。

方法 3 - 使用 ContinueWith

如果您只是按照设计使用 TPL 会怎样?没有元组。这里的想法是我们使用异常将 ContinueWith 重定向到两个不同的路径。

await DoAsync(name).ContinueWith(task =>
{
    if (task.Exception != null)
    {
        // handle fail
    }
    if (task.Result is StorageFile sf)
    {
        // handle success
    }
});

使用在出现任何类型的故障时抛出 exception 的方法。这与返回 boolean 不同。这是与 TPL 通信的一种方式。

async Task<StorageFile> DoAsync(string fileName)
{
    var folder = ApplicationData.Current.LocalCacheFolder;
    return await folder.GetFileAsync(fileName);
}

在上面的代码中,如果找不到文件,就会抛出异常。这将调用将在其逻辑块中处理 Task.Exception 的故障 ContinueWith。整齐吧?

听着,我们喜欢 Try 模式是有原因的。从根本上说,它非常简洁易读,因此是可维护的。当你选择你的方法时,看门狗的可读性。记住下一个开发人员,他在 6 个月内没有让你回答澄清问题。您的代码可能是开发人员将拥有的唯一文档。

祝你好运。


关于第三种方法,您确定链接 ContinueWith 调用具有预期结果吗?根据我的理解,第二个 ContinueWith 将检查第一个延续的成功,而不是原始任务的成功。
干杯@TheodorZoulias,那是一双敏锐的眼睛。固定的。
为流控制抛出异常对我来说是一种巨大的代码气味——它会降低你的性能。
不,@IanKemp,这是一个非常古老的概念。编译器已经进化。
我真的很喜欢这里的方法2。
J
Jpsy

这是为 C# 7.0 修改的 @dcastro 答案的代码,带有命名元组和元组解构,它简化了符号:

public async void Method1()
{
    // Version 1, named tuples:
    // just to show how it works
    /*
    var tuple = await GetDataTaskAsync();
    int op = tuple.paramOp;
    int result = tuple.paramResult;
    */

    // Version 2, tuple deconstruction:
    // much shorter, most elegant
    (int op, int result) = await GetDataTaskAsync();
}

public async Task<(int paramOp, int paramResult)> GetDataTaskAsync()
{
    //...
    return (1, 2);
}

有关新命名元组、元组文字和元组解构的详细信息,请参阅:https://blogs.msdn.microsoft.com/dotnet/2017/03/09/new-features-in-c-7-0/


T
Theodor Zoulias

async 方法不接受 out 参数的限制仅适用于编译器生成的异步方法,这些方法使用 async 关键字声明。它不适用于手工制作的异步方法。换句话说,可以创建接受 out 参数的 Task 返回方法。例如,假设我们已经有一个抛出的 ParseIntAsync 方法,并且我们想要创建一个不抛出的 TryParseIntAsync。我们可以这样实现它:

public static Task<bool> TryParseIntAsync(string s, out Task<int> result)
{
    var tcs = new TaskCompletionSource<int>();
    result = tcs.Task;
    return ParseIntAsync(s).ContinueWith(t =>
    {
        if (t.IsFaulted)
        {
            tcs.SetException(t.Exception.InnerException);
            return false;
        }
        tcs.SetResult(t.Result);
        return true;
    }, default, TaskContinuationOptions.None, TaskScheduler.Default);
}

使用 TaskCompletionSourceContinueWith 方法有点尴尬,但没有其他选择,因为我们不能在此方法中使用方便的 await 关键字。

使用示例:

if (await TryParseIntAsync("-13", out var result))
{
    Console.WriteLine($"Result: {await result}");
}
else
{
    Console.WriteLine($"Parse failed");
}

更新:如果异步逻辑过于复杂而无法在没有 await 的情况下表达,则可以将其封装在嵌套的异步匿名委托中。 out 参数仍需要 TaskCompletionSourceout 参数可能会在主任务完成之前完成,如下例所示:

public static Task<string> GetDataAsync(string url, out Task<int> rawDataLength)
{
    var tcs = new TaskCompletionSource<int>();
    rawDataLength = tcs.Task;
    return ((Func<Task<string>>)(async () =>
    {
        var response = await GetResponseAsync(url);
        var rawData = await GetRawDataAsync(response);
        tcs.SetResult(rawData.Length);
        return await FilterDataAsync(rawData);
    }))();
}

此示例假定存在三个连续调用的异步方法 GetResponseAsyncGetRawDataAsyncFilterDataAsyncout 参数在第二种方法完成时完成。 GetDataAsync 方法可以这样使用:

var data = await GetDataAsync("http://example.com", out var rawDataLength);
Console.WriteLine($"Data: {data}");
Console.WriteLine($"RawDataLength: {await rawDataLength}");

在这个简化的示例中,在等待 rawDataLength 之前等待 data 很重要,因为如果出现异常,out 参数将永远不会完成。


对于某些情况,这是一个非常好的解决方案。
P
Paul Marangoni

我认为像这样使用 ValueTuples 是可行的。您必须先添加 ValueTuple NuGet 包:

public async void Method1()
{
    (int op, int result) tuple = await GetDataTaskAsync();
    int op = tuple.op;
    int result = tuple.result;
}

public async Task<(int op, int result)> GetDataTaskAsync()
{
    int x = 5;
    int y = 10;
    return (op: x, result: y):
}

如果使用 .net-4.7 或 netstandard-2.0,则不需要 NuGet。
嘿,你是对的!我刚刚卸载了那个 NuGet 包,它仍然可以工作。谢谢!
t
themefield

对于真正希望将其保留在参数中的开发人员,这里可能是另一种解决方法。

将参数更改为数组或列表以包装实际值。请记住在发送到方法之前初始化列表。返回后,请务必在使用前检查值是否存在。谨慎编码。


P
Payam Buroumand

您可以通过使用 TPL(任务并行库)而不是直接使用 await 关键字来做到这一点。

private bool CheckInCategory(int? id, out Category category)
    {
        if (id == null || id == 0)
            category = null;
        else
            category = Task.Run(async () => await _context.Categories.FindAsync(id ?? 0)).Result;

        return category != null;
    }

if(!CheckInCategory(int? id, out var category)) return error

永远不要使用 .Result。这是一种反模式。谢谢!
此方法不是异步的。它没有回答这个问题。