当系统在C#winforms中睡眠时,如何触发system.threading.timer? [英] How trigger system.threading.timer when system sleep in C# winforms?

查看:77
本文介绍了当系统在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

推荐答案

最简单的方法是使用任务计划程序强制执行每晚午夜唤醒和跑步: http://www.thewindowsclub.com/wake-计算机从睡眠窗口 [ ^ ]

这样,你的应用程序甚至不必在PC进入睡眠状态之前运行 - 你可以告诉它在00:00唤醒,并运行你的app。

然后你的应用程序在完成后让它重新入睡。
The easiest way is to use the Task Scheduler to force a wakeup-and-run at midnight each night: http://www.thewindowsclub.com/wake-up-computer-from-sleep-windows[^]
That way, your app doesn't even have to be running before the PC enters Sleep - you can tell it to wake at 00:00, and run your app.
Then your app puts it back to sleep when it's finished.


这篇关于当系统在C#winforms中睡眠时,如何触发system.threading.timer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆