Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 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 13-Jan-13 2:32am
Comments
Sergey Alexandrovich Kryukov at 13-Jan-13 12:48pm
   
Really bad idea. And it won't improve security in any way.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.)
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 13-Jan-13 13:01pm
   
Agree, my 5.
But the problem still needs civilized solution. Please see my answer.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Comments
shaikh-adil at 15-Jan-13 8:31am
   
thank you sir. i will try first
Sergey Alexandrovich Kryukov at 15-Jan-13 10:38am
   
Great.
Good luck, call again.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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();
}
  Permalink  
Comments
shaikh-adil at 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)

  Print Answers RSS
0 Nirav Prabtani 284
1 OriginalGriff 217
2 Sergey Alexandrovich Kryukov 196
3 Mika Wendelius 185
4 CPallini 170
0 Nirav Prabtani 284
1 OriginalGriff 217
2 Mika Wendelius 185
3 _Amy 170
4 CPallini 170


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 13 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100