1 Star 0 Fork 8

zilin_zhang/fnsync

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
AutoDisposableTimer.cs 1.66 KB
一键复制 编辑 原始数据 按行查看 历史
holmium 提交于 2020-12-27 10:45 . Improves Performances
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace FnSync
{
class AutoDisposableTimer : IDisposable
{
private Timer timer = null;
public delegate void DisposedEventHandler(object sender, Object state);
public event DisposedEventHandler DisposedEvent;
private TimerCallback Callback = null;
private int DelayInMills = Timeout.Infinite;
public AutoDisposableTimer(TimerCallback callback, int DelayInMills, bool StartImmediately)
{
this.Callback = (state) =>
{
try
{
callback?.Invoke(state);
}
finally
{
Dispose();
}
};
this.DelayInMills = DelayInMills;
if (StartImmediately)
Start();
}
public void Start()
{
timer = new Timer(this.Callback, null, DelayInMills, Timeout.Infinite);
}
public AutoDisposableTimer(TimerCallback callback, int DelayInMills): this(callback, DelayInMills, true) {}
public void Dispose()
{
Dispose(null);
}
public void Dispose(Object state)
{
lock (this)
{
if (timer != null)
{
timer?.Dispose();
timer = null;
DisposedEvent?.Invoke(this, state);
}
}
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/zilin__zhang/fnsync.git
git@gitee.com:zilin__zhang/fnsync.git
zilin__zhang
fnsync
fnsync
master

搜索帮助