C# 定时无操作则退出登陆,回到登陆界面。

有时候根据需求需要为程序添加在规定的时间内无操作则退出当前的登陆程序的功能,如下代码模拟描述的需求功能。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace LockScreenMsg
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            label1.Height = 200;
        }

        [StructLayout(LayoutKind.Sequential)]
        struct LASTINPUTINFO
        {
            [MarshalAs(UnmanagedType.U4)]
            public int cbSize;
            [MarshalAs(UnmanagedType.U4)]
            public uint dwTime;
        }

        [DllImport("user32.dll")]
        static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);

        static long GetLastInputTime()
        {
            LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
            vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
            if (!GetLastInputInfo(ref vLastInputInfo))
            {
                return 0;
            }
            return Environment.TickCount - (long)vLastInputInfo.dwTime;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        public string isNull = null;
        private void timer1_Tick(object sender, EventArgs e)
        {
           long time=GetLastInputTime()/1000;
            
            this.label1.Text = string.Format("用户已经{0}秒没有操作了", time);
           if (time >= 20)
           {
                this.Close();
           }
        }
    }
}
posted @ 2017-02-23 13:49  蜗牛小传  阅读(1469)  评论(0编辑  收藏  举报

青春是什么?青春就像下面这段文字!

因为我处于页面最下端,所以很少有人看到我,因为我的字体小,所以即使别人看到了,也可能一扫而过,可能不会有人愿意停下来读一下我,对别人来讲,我就像一段无聊的广告一样放在这里,对我来讲,这就是我的青春。

我曾无数次问设计师,为什么不能把我放在首页最上方,为什么不能让所有人看到我?为什么我的青春不能像别人一样绽放?设计师从来没有回答过我的问题,实际上他从来都没听到过我的声音,你是否也和我一样?拥有梦想,

却不得不在人群中彷徨,拥有青春,却没有人愿意停下来为你鼓掌?