I have a main form that I load and when 20 minutes have passed I want a login form to open for security reasons. I want to verify that the proper user is using the software so I have thought about using a timer and give the timer an interval of 20 minutes. This is my code:
private void timer1_Tick_1(object sender, EventArgs e)
{
foreach (Form f in Application.OpenForms)
{
if (f.Name != "login2")
{
login2 lss = new login2();
lss.ShowDialog();
}
}
}
The problem here is that it opens the form every 20 minutes. This means that if the user is not doing anything, it is just showing one login form after another, causing multiple login forms to appear, which I do not want. I have used the loop but it still do not get it working properly. I do not know why.
apart from this can I implement the function that if the form is idle for 20 mints then show the login form and id not idle then don’t show the login form??
is it easy to implement? I don’t want any complication as I am a new comer to c# doing first time
First, add a function to reset your timer:
When called, it will reset the timer.
Second, to avoid having multiple dialogs opening, use the same instance. Add instance of that login form as a class member then use it instead of creating new one every time:
As you can see, it will call the dialog and only when it’s not already visible to avoid errror. It also reset the timer after user exit the dialog.
Finally, to have it count down only when “idle”, you need to reset the timer whenver there is any action. The simple case is mouse move and key press for every control in your form:
When inside your main form’s Load event this will assign the event handler for all controls, but if you have nested controls you’ll have to use recursion to get them all.