WinForm实现程序一段时间不运行自动关闭的方法

2019-12-26 13:26:35王冬梅

易采站长站为您分析WinForm实现程序一段时间不运行自动关闭的方法,涉及WinForm计时器及进程操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了WinForm实现程序一段时间不运行自动关闭的方法。。具体实现方法如下:

 

 
  1. using System;  using System.Collections.Generic; 
  2. using System.ComponentModel;  using System.Data; 
  3. using System.Drawing;  using System.Text; 
  4. using System.Windows.Forms;  using System.Net; 
  5. using System.IO;  using System.Security.Cryptography.X509Certificates; 
  6. using System.Net.Security;  namespace DemoDataGridView 
  7. {  public partial class Form3 : Form, IMessageFilter 
  8. {  private int m_WaitMinute = 0; 
  9. System.Windows.Forms.Timer MyTimer;  public Form3() 
  10. {  InitializeComponent(); 
  11. MyTimer = new Timer();  MyTimer.Interval = 1000; 
  12. MyTimer.Tick += new EventHandler(MyTimer_Tick);  Application.Idle += new EventHandler(Application_Idle); 
  13. }  void MyTimer_Tick(object sender, EventArgs e) 
  14. {  if (m_WaitMinute < 60) 
  15. {  MyTimer.Enabled = true; 
  16. MyTimer.Interval = 10000; //10秒  m_WaitMinute += 1; 
  17. // this.Opacity = 1.0 - Convert.ToDouble(m_WaitMinute / 60.0);  } 
  18. else  { 
  19. MyTimer.Enabled = false;  } 
  20. }  void Application_Idle(object sender, EventArgs e) 
  21. {  if (m_WaitMinute == 0) 
  22. {  System.IO.File.WriteAllText("D:1.txt", DateTime.Now.ToString()); 
  23. MyTimer.Start();  } 
  24. else  { 
  25. if (m_WaitMinute >= 6)  { 
  26. System.IO.File.WriteAllText("D:2.txt", DateTime.Now.ToString());  this.Close(); 
  27. }  } 
  28. }  public bool PreFilterMessage(ref Message m)