gpt4 book ai didi

c# - 将异步回调传递给 Timer 构造函数

转载 作者:可可西里 更新时间:2023-11-01 07:51:29 26 4
gpt4 key购买 nike

我有异步回调,它被传递到 Timer(来自 System.Threading)构造函数:

private async Task HandleTimerCallback(object state)
{
if (timer == null) return;

if (asynTaskCallback != null)
{
await HandleAsyncTaskTimerCallback(state);
}
else
{
HandleSyncTimerCallback(state);
}
}

和计时器:

timer = new Timer(async o => await HandleTimerCallback(o), state, CommonConstants.InfiniteTimespan,
CommonConstants.InfiniteTimespan);

有没有办法在 lambda 中省略 o 参数?非异步的原因我可以将我的 handler 作为委托(delegate)传递

 timer = new Timer(HandleTimerCallback, state, CommonConstants.InfiniteTimespan,
CommonConstants.InfiniteTimespan);

最佳答案

Is there any way to omit that o param in lambda?

当然,只需将您的事件处理程序方法定义为async void:

private async void HandleTimerCallback(object state)

关于c# - 将异步回调传递给 Timer 构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38917818/

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