WinForm程序 一段时间不运行自动退出

-----------------------------------------------------------------------------

应用场景:实现程序一段时间不运行自动关闭的方法

============================================

功能实现代码如下所示:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Text;
 7 using System.Windows.Forms;
 8 using System.Net;
 9 using System.IO;
10 using System.Security.Cryptography.X509Certificates;
11 using System.Net.Security;
12 
13 namespace DemoDataGridView
14 {
15  public partial class Form1 : Form, IMessageFilter
16  {
17   private int m_WaitMinute = 0;
18   System.Windows.Forms.Timer MyTimer;
19   public Form3()
20   {
21    InitializeComponent();
22    MyTimer = new Timer();
23    MyTimer.Interval = 1000;
24    MyTimer.Tick += new EventHandler(MyTimer_Tick);
25    Application.Idle += new EventHandler(Application_Idle);
26   }
27   void MyTimer_Tick(object sender, EventArgs e)
28   {
29    if (m_WaitMinute < 60)
30    {
31     MyTimer.Enabled = true;
32     MyTimer.Interval = 10000; //10秒
33     m_WaitMinute += 1;
34     // this.Opacity = 1.0 - Convert.ToDouble(m_WaitMinute / 60.0);
35    }
36    else
37    {
38     MyTimer.Enabled = false;
39    }
40   }
41   void Application_Idle(object sender, EventArgs e)
42   {
43    if (m_WaitMinute == 0)
44    {
45     System.IO.File.WriteAllText("D:\\1.txt", DateTime.Now.ToString());
46     MyTimer.Start();
47    }
48    else
49    {
50     if (m_WaitMinute >= 6)
51     {
52      System.IO.File.WriteAllText("D:\\2.txt", DateTime.Now.ToString());
53      this.Close();
54     }
55    }
56   }
57   public bool PreFilterMessage(ref Message m)
58   {
59    if (m_WaitMinute != 0)
60    {
61     m_WaitMinute = 0;
62     MyTimer.Enabled = false;
63     return true;
64    }
65    return false;
66   }
67  }
68 }

 

posted @ 2023-02-10 09:59  DotNet码农  阅读(349)  评论(0编辑  收藏  举报
/*粒子线条,鼠标移动会以鼠标为中心吸附的特效*/