gpt4 book ai didi

c# - AsyncCallback 通过引用变量取值

转载 作者:太空宇宙 更新时间:2023-11-03 14:15:55 24 4
gpt4 key购买 nike

我需要使用异步委托(delegate)调用一个函数,当我浏览 AsyncCallback 的教程时,我看到异步回调定义如下:

static void CallbackMethod(IAsyncResult result)
{
// get the delegate that was used to call that
// method
CacheFlusher flusher = (CacheFlusher) result.AsyncState;

// get the return value from that method call
int returnValue = flusher.EndInvoke(result);

Console.WriteLine("The result was " + returnValue);
}

如果我可以从函数中获取返回值作为引用,请告诉我。例如:=我的函数格式为

void GetName(int id,ref string Name);

在这里,我通过引用变量获取函数的输出。如果我使用异步委托(delegate)调用此函数,我该如何读取回调函数的输出?

最佳答案

您需要将参数包装到一个对象中:

class User
{
public int Id { get; set; }

public string Name { get; set; }
}

void GetName(IAsyncResult result)
{
var user = (User)result.AsyncState
// ...
}

AsyncCallback callBack = new AsyncCallback(GetName);

关于c# - AsyncCallback 通过引用变量取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6582603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com