0

下面的 c# 代码绝不是理想的,我真的只是在寻找关于如何最好地重构它并使代码更安全的建议和建议。

基本上有一个类变量存储安全检查阶段的值(初始化为 0)。当按下我的应用程序中的按钮时,将运行以下代码以检查用户是否有权访问其帐户屏幕。根据方法参数,调用适当的处理程序方法,该方法向用户显示 PIN 输入用户控件(此用户控件是显示全屏和最顶部的自定义控件)。当处理程序代码运行时,下面显示的代码在 do while 循环中调用 Application.DoEvents,以在用户输入其 PIN 时保持一切响应。如果 do while 循环不存在,则在我们有机会验证用户 PIN 是否正确之前,用户尝试访问的屏幕将出现在 PIN 输入屏幕的顶部。

            try
            {
                this.Cursor = Cursors.WaitCursor;

                Application.DoEvents();

                SecurityCheckStage = 0;

                Security             = new tskSecurity(true);
                Security.TaskUpdate += new TaskUpdateHandler(_handler);

                TaskManager.AddTask(Security, true);

                this.Cursor = Cursors.Default;

                // Wait until security check has passed before showing account screen
                do
                {
                    Application.DoEvents();
                    System.Threading.Thread.Sleep(100);
                }
                while (SecurityCheckStage == 0);

                if (SecurityCheckStage == 1) ShowAccountScreen();

                return false;
            }
            catch
            {
                throw;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }

我知道Application.DoEvents()循环调用不是一个好习惯,所以我真的想重新编写这段代码以使其更好。

任何帮助将非常感激。请记住,该问题的解决方案必须适用于 .NET 3.5 Framework。

4

1 回答 1

1

使用System.Windows.Forms.Timer... 例如:

//...

    timer.Tick += TimerEventProcessor;
    timer.Start();
//..
private void TimerEventProcessor(Object sender, EventArgs myEventArgs)
{
    if (SecurityCheckStage == 1)
    {
        var timer = (Timer) sender;
        timer.Stop();
        ShowAccountScreen();
    }
}
于 2014-03-27T13:56:41.217 回答