WinForm实现程序一段时间不运行自动关闭的方法
本文实例讲述了WinForm实现程序一段时间不运行自动关闭的方法。分享给大家供大家参考。具体实现方法如下:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Net;
usingSystem.IO;
usingSystem.Security.Cryptography.X509Certificates;
usingSystem.Net.Security;
namespaceDemoDataGridView
{
publicpartialclassForm3:Form,IMessageFilter
{
privateintm_WaitMinute=0;
System.Windows.Forms.TimerMyTimer;
publicForm3()
{
InitializeComponent();
MyTimer=newTimer();
MyTimer.Interval=1000;
MyTimer.Tick+=newEventHandler(MyTimer_Tick);
Application.Idle+=newEventHandler(Application_Idle);
}
voidMyTimer_Tick(objectsender,EventArgse)
{
if(m_WaitMinute<60)
{
MyTimer.Enabled=true;
MyTimer.Interval=10000;//10秒
m_WaitMinute+=1;
//this.Opacity=1.0-Convert.ToDouble(m_WaitMinute/60.0);
}
else
{
MyTimer.Enabled=false;
}
}
voidApplication_Idle(objectsender,EventArgse)
{
if(m_WaitMinute==0)
{
System.IO.File.WriteAllText("D:\\1.txt",DateTime.Now.ToString());
MyTimer.Start();
}
else
{
if(m_WaitMinute>=6)
{
System.IO.File.WriteAllText("D:\\2.txt",DateTime.Now.ToString());
this.Close();
}
}
}
publicboolPreFilterMessage(refMessagem)
{
if(m_WaitMinute!=0)
{
m_WaitMinute=0;
MyTimer.Enabled=false;
returntrue;
}
returnfalse;
}
}
}
希望本文所述对大家的C#程序设计有所帮助。