Complete "Logon/main form/user operation activation listener/automatic logout" Model

Source: Internet
Author: User
The complete "Logon/main form/user operation activation listener/automatic logout" model can automatically log out of the system when no operation is performed by the user within the specified time. It is consistent with the event registration framework, the application can be expanded on it.

// Logon form
Public partial class login: Form
{
// Single-piece Mode
Protected static login instance;
Public static login create ()
{
If (instance = NULL)
Instance = New Login ();
Return instance;
}
Private login ()
{
Initializecomponent ();
}
Private void login_load (Object sender, eventargs E)
{
// Register event processing
Commandhelper. registeclickevent (this. btnlogin, delegate (Object o, eventargs P)
{

// Handle logon key events
// User verification
...
// Present the main form
Mainform. Create (). Show ();
// Enable the operation activation listener
Commandhelper. Required mactiveevent = true;
// Reset the counter Logic
Commandhelper. resettimeout ();
// Hide the logon form
This. Hide ();

}
);

}

}
// Main form
Public partial class mainform: Form
{
// Single-piece Mode
Protected static mainform instance;
Public static mainform create ()
{
If (instance = NULL)
Instance = new mainform ();
Return instance;
}
Private mainform ()
{
Initializecomponent ();
}
/// <Summary>
/// Cross-thread Secure Access Model Code
/// </Summary>
Private delegate void onnoactiontimeouthandler ();
/// <Summary>
/// Cross-thread Secure Access Model Code
/// </Summary>
Private onnoactiontimeouthandler = new onnoactiontimeouthandler (performnoactiontimeout );
/// <Summary>
/// Cross-thread Secure Access Model Code
/// </Summary>
Private Static void extends mnoactiontimeout ()
{
// Disable the operation to activate the listener
Commandhelper. Required mactiveevent = false;
// Display the logon form
Login. Create (). Show ();
// Hide the main form
Mainform. Create (). Hide ();
}
Private void mainform_load (Object sender, eventargs E)
{
// Exit the application when the user closes the main form
Commandhelper. registeformcolsingevent (this, delegate (Object o, formclosingeventargs ARGs)
{
Environment. Exit (0 );
});
// Register the operation activation listener event: if the user does not perform any operation, the event is automatically canceled 60 seconds later.
Commandhelper. registhandlewhennoactiontimeout (60, delegate (Object o, system. Timers. elapsedeventargs P)
{
// Asynchronous encapsulation model
Begininvoke (onnoactiontimeouthandler );
}
);
}
}
// Command help class
Public class commandhelper
{
/// <Summary>
/// Whether to enable the operation activation listener
/// </Summary>
Protected static bool extends mactiveevent = false;
/// <Summary>
/// Whether to enable the operation activation listener
/// </Summary>
Public static bool initialize mactiveevent {get {return initialize mactiveevent;} set {initialize mactiveevent = value ;}}
/// <Summary>
/// Clock object
/// </Summary>
Protected static system. Timers. Timer taskinvoker = new system. Timers. Timer ();
Protected static double interval = 60000d;
/// <Summary>
/// The operation that will be called after the user does not perform any operation at the specified interval (s)
/// </Summary>
/// <Param name = "second"> specified time interval (s) </param>
/// <Param name = "handler"> called Operation </param>
Public static void registhandlewhennoactiontimeout (INT second, system. Timers. elapsedeventhandler handler)
{
System. reflection. eventinfo = taskinvoker. getType (). getevent ("elapsed", system. reflection. bindingflags. instance | system. reflection. bindingflags. static | system. reflection. bindingflags. public );
Eventinfo. addeventhandler (taskinvoker, Handler );
Interval = Second * 1000;
Taskinvoker. interval = interval;
If (initialize mactiveevent)
Taskinvoker. Start ();
}

/// <Summary>
/// Reset the counter triggered by the command event
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Public static void inclumeventhandler (Object sender, eventargs E)
{
// Capture the status of user operations in this method;
If (initialize mactiveevent)
{
Resettimeout ();
}
}
/// <Summary>
/// Reset the counting Logic
/// </Summary>
Public static void resettimeout ()
{
Taskinvoker. Stop ();
Taskinvoker. interval = interval;
Taskinvoker. Start ();
}

/// <Summary>
/// Register a click event
/// </Summary>
/// <Param name = "command"> command object </param>
/// <Param name = "handler"> response DeleGate </param>
Public static void registeclickevent (Object commander, system. eventhandler handler)
{
System. reflection. eventinfo = commander. getType (). getevent ("click", system. reflection. bindingflags. instance | system. reflection. bindingflags. static | system. reflection. bindingflags. public );
If (eventinfo! = NULL)
{
Eventinfo. addeventhandler (Commander, Handler );
Eventinfo. addeventhandler (Commander, new system. eventhandler (inclumeventhandler ));
}
}
/// <Summary>
/// Register an event
/// </Summary>
/// <Param name = "Commander"> </param>
/// <Param name = "handler"> </param>
/// <Param name = "eventname"> </param>
Public static void registeevent (Object commander, system. eventhandler handler, string eventname)
{
System. reflection. eventinfo = commander. getType (). getevent (eventname, system. reflection. bindingflags. instance | system. reflection. bindingflags. static | system. reflection. bindingflags. public );
If (eventinfo! = NULL)
{
Eventinfo. addeventhandler (Commander, Handler );
Eventinfo. addeventhandler (Commander, new system. eventhandler (inclumeventhandler ));
}
}
/// <Summary>
/// Register the Close event
/// </Summary>
/// <Param name = "Commander"> </param>
/// <Param name = "handler"> </param>
Public static void registeformcolsingevent (Object commander, formclosingeventhandler handler)
{
System. reflection. eventinfo = commander. getType (). getevent ("formclosing", system. reflection. bindingflags. instance | system. reflection. bindingflags. static | system. reflection. bindingflags. public );
If (eventinfo! = NULL)
{
Eventinfo. addeventhandler (Commander, Handler );
Eventinfo. addeventhandler (Commander, new formclosingeventhandler (inclumeventhandler ));
}
}
// Other events
....
}

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.