通过计时器事件显示登录表单 [英] Showing login form through timer event

查看:69
本文介绍了通过计时器事件显示登录表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主要表单,我加载,当20分钟过去时,我想要一个登录表单打开出于安全原因。我想验证正确的用户正在使用该软件,所以我考虑过使用计时器并给定时器一个20分钟的间隔。这是我的代码:



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();
        }
    }
}





这里的问题是它每20分钟打开一次表格。这意味着如果用户没有做任何事情,它只是显示一个接一个的登录表单,导致出现多个登录表单,这是我不想要的。我已经使用了循环,但它仍然无法使其正常工作。我不知道为什么。



除此之外,我可以表示如果表格闲置20分钟,那么显示登录表格并且不是闲置然后不显示登录表单?是否易于实施?我不想要任何复杂因素,因为我是c#第一次做的新来者

推荐答案

有几个原因。
第一个很简单:如果列表中的第一个表单不是 login2 的实例会怎么样?



由于它的名字不是login2,你将创建一个表格并显示它。



但它变得更糟!因为您使用的是ShowDialog,所以在表单关闭之前它不会返回。当你关闭那个实例时,它将查看下一个实例,如果那也不是login2,它会再次显示它。当用户关闭所有opf时,计时器可能再次嘀嗒,整个快乐追逐再次开始...



停止你正在做的事情。回过头来思考这一切,以及你想要实现的目标。不是我想显示登录表单,而是您尝试显示表单的原因。你应该怎么做?原始表格会发生什么?有没有更好的方法来处理这一切?这是否适用于其他应用程序?





(提示:是的,还有更好的方法!但是我想让你考虑一下。 )
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.)


正如我所说,你的想法非常糟糕。你可以在解决方案2中得到一些解释,但是根本没有使用计时器的原因有很多,尤其是这个计时器类,并且从不以你想要的方式使用它。这不是进一步了解它的地方。



我明白你对安全性的考虑是这样的:你想要在合法的认证用户时防止这种情况离开工作地点一段时间,其他人可以在此期间进行一些非法活动。您希望通过确认身份验证来减少这种机会。



您可以这样做,但您需要处理不同的事件:当用户尝试提供任何事件时调用的事件输入到应用程序。例如, TextBox.TextChanged CheckBox.CheckStateChanged 等。在最坏的情况下或为了确保可靠性,您可以处理所有鼠标和键盘事件。在所有事件中,您首先检查先前身份验证的垃圾邮件时间(不需要计时器,只需 System.DateTime !),默认情况下这次工作不会过期。



-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


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();
}


这篇关于通过计时器事件显示登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆