Recently in doing a courier cabinet project, require the user no operation time to a minute, automatically return to the homepage, I then encapsulated a countdown control, nonsense less, directly on the code
Public partial class Remainingtimeuc:usercontrol {public Remainingtimeuc () {initializecom Ponent (); } Form parantform = null; private void Remainingtimeuc_load (object sender, EventArgs e) {//Get the user control's long lift parantform = thi S.findform (); } #region Timeruc_tick Event private void Timeruc_tick (object sender, EventArgs e) {try {int totalsecond = 60;//set time is 60s var remaintime = totalsecond-mousekeyboardoperate. Getlastinputtime ();//Total time-user not operating time This.labRT.Text = remaintime.tostring (CultureInfo.InvariantCulture);//Display off The remaining time to close if (Remaintime = =) {This.labRT.ForeColor = Color.orange; } if (Remaintime <= 9) {this.labRT.ForeColor = color.red; This.labRT.Text = "0" + remaintime; } if (remaintime = =) This.labRT.ForeColor = Color.White; if (Remaintime = = 0) {parantform.close (); }} catch (Exception ex) {Loghelper.writelog (this. Name + "Timeruc_tick", ex. Message); }} #endregion}
//
This method does not know which predecessor wrote, borrow a bit
//
public class Mousekeyboardoperate
{
<summary>
Create struct to return capture time
</summary>
[StructLayout (LayoutKind.Sequential)]
struct LASTINPUTINFO
{
<summary>
Set structure block capacity
</summary>
[MarshalAs (UNMANAGEDTYPE.U4)]
public int cbsize;
<summary>
Captured by Time
</summary>
[MarshalAs (UNMANAGEDTYPE.U4)]
public UINT Dwtime;
}
[DllImport ("User32.dll")]
private static extern bool Getlastinputinfo (ref lastinputinfo plii);
<summary>
Get keyboard and mouse time without action
</summary>
<returns> the time interval that the user last used the system to now, in seconds </returns>
public static long Getlastinputtime ()
{
var vlastinputinfo = new Lastinputinfo ();
Vlastinputinfo.cbsize = marshal.sizeof (Vlastinputinfo);
if (! Getlastinputinfo (ref Vlastinputinfo))
{
return 0;
}
Else
{
var count = Environment.tickcount-(long) vlastinputinfo.dwtime;
var icount = count/1000;
Return icount;
}
}
Winfrom Countdown Control