Sandeep Choudary

Sandeep Choudary

  • NA
  • 79
  • 9.8k

How to set a timer in Windows application when user is idle

May 25 2020 6:30 PM
I have a question in Windows Forms on setting timer when the user is idle or Inactive. I need the timer to set even on any Mouse Events. If the user makes any moment then I need to reset the timer. So this is the requirement. Here goes the code.
  1. using System;  
  2. using System.Windows.Forms;  
  3. using Timer = System.Windows.Forms.Timer;  
  4. namespace FormsTimerSetup.Globals  
  5. {  
  6. public class SetApplicationTimeOut : Form  
  7. {  
  8. #region  
  9. ///  
  10. /// Private Timer Property  
  11. ///  
  12. private static Timer _timer;  
  13. ///  
  14. /// Timer Property  
  15. ///  
  16. public static Timer Timer  
  17. {  
  18. get  
  19. {  
  20. return _timer;  
  21. }  
  22. set  
  23. {  
  24. if (_timer != null)  
  25. {  
  26. _timer.Tick -= Timer_Tick;  
  27. }  
  28. _timer = value;  
  29. if (_timer != null)  
  30. {  
  31. _timer.Tick += Timer_Tick;  
  32. }  
  33. }  
  34. }  
  35. #endregion  
  36. #region Events  
  37. public event EventHandler UserActivity;  
  38. #endregion  
  39. #region Constructor  
  40. /// Default/Parameterless SetApplicationTimeOut Constructor  
  41. public SetApplicationTimeOut()  
  42. {  
  43. KeyPreview = true;  
  44. FormClosed += ObservedForm_FormClosed;  
  45. MouseMove += ObservedForm_MouseMove;  
  46. KeyDown += ObservedForm_KeyDown;  
  47. }  
  48. #endregion  
  49. #region Inherited Methods  
  50. protected virtual void OnUserActivity(EventArgs e)  
  51. {  
  52. // Invoking the UserActivity delegate  
  53. UserActivity?.Invoke(this, e);  
  54. }  
  55. public void SetTimeOut()  
  56. {  
  57. // postpone auto-logout by 30 minutes  
  58. _timer = new Timer  
  59. {  
  60. Interval = (30 * 60 * 1000) // Timer set for 30 minutes  
  61. };  
  62. Application.Idle += Application_Idle;  
  63. _timer.Tick += new EventHandler(Timer_Tick);  
  64. }  
  65. #endregion  
  66. #region Private Methods  
  67. private void ObservedForm_MouseMove(object sender, MouseEventArgs e)  
  68. {  
  69. OnUserActivity(e);  
  70. }  
  71. private void ObservedForm_KeyDown(object sender, KeyEventArgs e)  
  72. {  
  73. OnUserActivity(e);  
  74. }  
  75. private void ObservedForm_FormClosed(object sender, FormClosedEventArgs e)  
  76. {  
  77. FormClosed -= ObservedForm_FormClosed;  
  78. MouseMove -= ObservedForm_MouseMove;  
  79. KeyDown -= ObservedForm_KeyDown;  
  80. }  
  81. private static void Application_Idle(object sender, EventArgs e)  
  82. {  
  83. _timer.Stop();  
  84. _timer.Start();  
  85. }  
  86. private static void Timer_Tick(object sender, EventArgs e)  
  87. {  
  88. _timer.Stop();  
  89. Application.Idle -= Application_Idle;  
  90. MessageBox.Show("Application Terminating");  
  91. Application.Exit();  
  92. }  
  93. #endregion  
  94. }  
  95. }  
I have implemented the code but unsure whether it is the right way of doing it.
 
Any leads would be appreciated. Thanks for going through the post and STAY SAFE!

Answers (2)