Click here to Skip to main content
16,018,092 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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:

C#
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 impliment the fucntion that if the form is idle for 20 mints then show the login form and id not idle then dont show the login form?? is it easy to implement? i dont want any complication as i am a new comer to c# doing first time
Posted
Comments
Sergey Alexandrovich Kryukov 13-Jan-13 12:48pm    
Really bad idea. And it won't improve security in any way.
—SA

There are a couple of reasons for that.
The first one is simple: what happens if the first form in the list is not an instance of login2?

Since it's name isn't login2, you will create a form and display it.

But it gets worse! Because you are using ShowDialog it doesn't return until the form is closed. When you close that instance it will look at the next, and if that isn't login2 either, it displays it again. By the time the user has finished closing all opf them, the timer probably Ticks again, and the whole merry chase starts again...

Stop what you are doing. Go back and think about all this, and what you are trying to achieve. Not "I want to display a login form" but why you are trying to display a form. What should you be doing? What should happen to the original form? Is there a better way to handle all this? Is this the way it works in other applications?


(HINT: Yes, there is a better way! But I'd like you to think about it for a moment.)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Jan-13 13:01pm    
Agree, my 5.
But the problem still needs civilized solution. Please see my answer.
—SA
As I say, your idea is really bad. Your can get some explanation in Solution 2, but there are more reasons for not using the timer at all, especially this timer class, and never using it the way you wanted. This is not the place for going any further about it.

I understand that your thinking about security is this: you want to prevent the cases, when a legitimate authenticated user leaves the working place for a while, some other person can conduct some illegal activity during this time. You want to reduce this chance by confirmation of authentication.

You can do it, but you need to handle different events: the events invoked when a user tried to provide any input to the application. For example, TextBox.TextChanged, CheckBox.CheckStateChanged and the like. In worst case or to ensure dependability, you can eve handle all mouse and keyboard events. In all events, you first checkup the time spam from previous authentication (no timer is needed, only System.DateTime!) and work by default it this time is not expired.

—SA
 
Share this answer
 
Comments
shaikh-adil 15-Jan-13 8:31am    
thank you sir. i will try first
Sergey Alexandrovich Kryukov 15-Jan-13 10:38am    
Great.
Good luck, call again.
—SA
C#
private void timer1_Tick_1(object sender, EventArgs e)
{
    //Stop the timer
    timer1.Stop();

    //Do your login verification here

    //Re-start the timer
    timer1.Start();
}
 
Share this answer
 
Comments
shaikh-adil 15-Jan-13 8:30am    
thank you sir

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900