C # timeout lock

Source: Internet
Author: User

[Csharp]
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 VistaRenderer
{
Public partial class FrmTestTimeout: Form
{
Public FrmTestTimeout ()
{
InitializeComponent (); // this. timer1 = new System. Windows. Forms. Timer (this. components );
}
 
Private void FrmTestTimeout_Load (object sender, EventArgs e)
{
Timer1.Enabled = true;
Timer1.Interval = 2*1000; // timer trigger interval of 2 seconds
This. timer1.Tick + = new EventHandler (timerstmtick );
}
/// <Summary>
/// Timer triggered
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Void timereffectick (object sender, EventArgs e)
{
// The last input time is greater than 5 seconds.
If (GetLastInputTime ()> = 5*1000)
{
MessageBox. Show ("time out"); // write timeout is allowed here. For example, a lock window is displayed.
}
}
 
[StructLayout (LayoutKind. Sequential)]
Struct LASTINPUTINFO
{
[Financialas (UnmanagedType. U4)]
Public int cbSize;
[Financialas (UnmanagedType. U4)]
Public uint dwTime;
}
 
[DllImport ("user32.dll")]
Static extern bool GetLastInputInfo (ref LASTINPUTINFO plii );
 
/// <Summary>
/// Obtain the interval from the last operation
/// </Summary>
/// <Returns> </returns>
Static long GetLastInputTime ()
{
LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO ();
VLastInputInfo. cbSize = Marshal. SizeOf (vLastInputInfo );
If (! GetLastInputInfo (ref vLastInputInfo) return 0;
Return Environment. TickCount-(long) vLastInputInfo. dwTime;
}
 

}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.