将异步回调传递给Timer构造函数 [英] Pass async Callback to Timer constructor
本文介绍了将异步回调传递给Timer构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个异步回调,该回调传递给Timer(来自System.Threading)构造函数:
I have async callback, which is passed into Timer(from System.Threading) constructor :
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
作为委托通过
Is there any way to omit that o
param in lambda? Cause for not-async I can just pass my handler
as delegate
timer = new Timer(HandleTimerCallback, state, CommonConstants.InfiniteTimespan,
CommonConstants.InfiniteTimespan);
推荐答案
有什么方法可以省略lambda中的o参数吗?
Is there any way to omit that o param in lambda?
当然,只需将事件处理程序方法定义为async void
:
Sure, just define your event handler method as async void
:
private async void HandleTimerCallback(object state)
这篇关于将异步回调传递给Timer构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文