【发布时间】:2017-05-31 04:24:14
【问题描述】:
在 ASP.NET Core 之前,我曾经实现过这样的计时器:
public class Class1
{
Timer tm = null;
public Class1()
{
this.tm.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
this.tm.AutoReset = true;
this.tm = new Timer(60000);
}
protected void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
this.tm.Stop();
try
{
// My business logic here
}
catch (Exception)
{
throw;
}
finally
{
this.tm.Start();
}
}
}
我对 .NETCoreApp1.1 控制台应用程序有同样的需求,而 System.Timers 在 ASP.NET Core 中不再存在。我也尝试使用 System.Threading.Timer 但该项目不再构建。
如何使用 .NETCoreApp1.1 实现 Timer?是否有 System.Timers 的等价物?
【问题讨论】:
-
System.Threading.Timer 确实支持 .NET core >= 1.1.0 参考依赖section
-
@Sanket 它是兼容的,但我必须在 Framework 部分添加对 Microsoft.NETCore.App 的依赖项
标签: c# timer asp.net-core-1.1