本文介绍了当系统在C#winforms中睡眠时,如何触发system.threading.timer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的要求是在午夜12点午清除datagird视图中的一些数据。它是工作系统不处于挂起状态(睡眠)。如何在系统睡眠时触发System.Threading.Timer?
我尝试过:
My requirement is to clear some data shows in datagird view when at 12 pm midnight. it is working system not in suspend state(Sleep). How can trigger System.Threading.Timer when system sleep?
What I have tried:
System.Threading.Timer timer;
public void PurgeData(DateTime dt)
{
try
{
WallBoardApp.LogInfo("Purge started");
if (WallBoardApp.Instance.ObjSettings.IsPurgeDataonMidnight)
{
WallBoardApp.LogInfo("WallBoardApp.Instance.ObjSettings.IsPurgeDataonMidnight");
System.Threading.TimerCallback callback = new TimerCallback(ProcessTimerEvent);
bool is24HourTime = System.Globalization.DateTimeFormatInfo.CurrentInfo.ShortTimePattern.Contains("H");
if (is24HourTime)
{
if (DateTime.Now < dt)
{
WallBoardApp.LogInfo("DateTime.Now < dt" + dt + DateTime.Now.ToLocalTime() + "24 hour format");
timer = new System.Threading.Timer(callback, null,
(dt - DateTime.Now.ToLocalTime()),TimeSpan.FromHours(24));
}
}
else
{
WallBoardApp.LogInfo("DateTime.Now < dt" + dt + DateTime.Now.ToLocalTime() + "12 hour format");
timer = new System.Threading.Timer(callback, null,
(dt - DateTime.Now.ToLocalTime()), TimeSpan.FromHours(12));
}
}
}
catch (Exception ex)
{
WallBoardApp.LogException("UnityApplicationSuite", "PurgeData()", ex);
}
}
private void ProcessTimerEvent(object obj)
{
try
{
WallBoardApp.LogInfo("Purge ProcessTimerEvent started");
ClearWallboardData();
WallBoardApp.LogInfo("Clear wallboard data");
NetworkLost();
WallBoardApp.LogInfo("Purge data finished");
WallBoardApp.LogInfo("Auto restart At purge Executing.");
if (WallBoardApp.Instance.ObjUpdate.IsDownloadComplete && WallBoardApp.Instance.ObjSettings.IsAutoUpdate && WallBoardApp.Instance.ObjSettings.IsAutoRestartAtPurge)
{
WallBoardApp.LogInfo("Auto restart At purge Executed.");
Application.Restart();
Application.Exit();
}
PurgeData(new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59));
WallBoardApp.LogInfo("Purge data started");
}
catch (Exception ex)
{
}
}
在上面的代码中,ProcessTimerEvent在午夜触发
In the above code ProcessTimerEvent is trigger at midnight
推荐答案
这篇关于当系统在C#winforms中睡眠时,如何触发system.threading.timer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!