1 Star 0 Fork 729

EricZheng/dgiot_dtu

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
TimerHelper.cs 1.75 KB
一键复制 编辑 原始数据 按行查看 历史
lsxredrain 提交于 2021-12-20 09:48 . add groupFlag
// <copyright file="TimerHelper.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
using System;
using System.Collections.Generic;
using System.Threading;
namespace Dgiot_dtu
{
public class TimerHelper
{
private TimerHelper()
{
}
private static TimerHelper instance;
private static Dictionary<string, Timer> timers = new Dictionary<string, Timer> { };
public static TimerHelper GetInstance()
{
if (instance == null)
{
instance = new TimerHelper();
}
return instance;
}
public static void Start(string key, object state = null, int period = 1000, int dueTime = Timeout.Infinite )
{
if (timers.ContainsKey(key))
{
try
{
timers[key].Change(0, 1000);
}
catch
{
}
}
TimerCallback callback = new TimerCallback(ReadValue);
Timer timer = new Timer(callback, state, dueTime, period);
timer.InitializeLifetimeService();
timers.Add(key, timer);
}
public static void Stop(string key)
{
if (timers.ContainsKey(key))
{
try
{
timers[key].Change(0, 1000);
}
catch
{
}
}
}
public static void ReadValue(object state)
{
LogHelper.Log("GetState " + state.ToString());
GC.Collect();
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/eric3495/dgiot_dtu.git
git@gitee.com:eric3495/dgiot_dtu.git
eric3495
dgiot_dtu
dgiot_dtu
master

搜索帮助