C# 奇怪的开始发疯的行为#

C# 奇怪的开始发疯的行为#,c#,winforms,C#,Winforms,在我的一款C#4.0 windows应用程序中,我面临着(很可能)BeginInvoke的异常行为。我有一个登录表单,每次用户尝试登录时都会生成一个ID,ID授权会写入一个文件,该登录表单正在使用FileSystemWatcher监视。在授权时,将生成一个事件,该事件由主窗体捕获 现在我的问题是,旧ID会在验证时显示,即使每次登录过程中都会创建一个新的LoginForm实例。strID变量中的ID值第一次是正确的,但从那时起,每次连续登录都会显示以前的ID 我很难用文字正确解释,所以我附上了登录

在我的一款C#4.0 windows应用程序中,我面临着(很可能)BeginInvoke的异常行为。我有一个登录表单,每次用户尝试登录时都会生成一个ID,ID授权会写入一个文件,该登录表单正在使用FileSystemWatcher监视。在授权时,将生成一个事件,该事件由主窗体捕获

现在我的问题是,旧ID会在验证时显示,即使每次登录过程中都会创建一个新的LoginForm实例。strID变量中的ID值第一次是正确的,但从那时起,每次连续登录都会显示以前的ID

我很难用文字正确解释,所以我附上了登录表单的代码。我面临的奇怪行为是在
if(arrData[0]==this.strID)
函数的
GetAuthorizationStatus
部分。请帮助我了解这是一种预期行为还是我在这里做错了什么

登录表单有3个按钮:btnLogin用于获取ID,btnExit用于退出表单,btnAuth用于验证(我把它放在这里是为了测试,而不是另一个应用程序/服务)

下面是代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace SampleInvokeTest
{
    public class AuthEventArgs : EventArgs
    {
        private string strID;
        private DateTime dtLoginAt;
        private bool isAuthorized;

        public AuthEventArgs(string ID, DateTime LoginAt, bool isAuthorized)
        {
            this.strID = ID;
            this.dtLoginAt = LoginAt;
            this.isAuthorized = isAuthorized;
        }

        public string ID
        { get{ return this.strID; } }

        public DateTime LoginAt
        { get{ return this.dtLoginAt; } }

        public bool IsAuthorized
        { get{ return this.isAuthorized; } }
    }

    public partial class LoginForm : Form
    {
        delegate void ShowDelegate();

        private string strID = "";
        private const string FILE_PATH = @"E:\test.txt";
        private DateTime dtLastResultReadTime = DateTime.MinValue;
        private DateTime dtLoginAt = DateTime.MinValue;
        private bool isAuthorized = false;
        private string strLoginErrorMessage ="";

        public event EventHandler<AuthEventArgs> AuthOccurred; 

        public LoginForm()
        {
            InitializeComponent();

            FileSystemWatcher fswAuth = new FileSystemWatcher(Path.GetDirectoryName(FILE_PATH));
            fswAuth.Filter = Path.GetFileName(FILE_PATH);
            fswAuth.NotifyFilter = NotifyFilters.LastWrite;
            fswAuth.Changed+= new FileSystemEventHandler(fswAuth_Changed);
            fswAuth.EnableRaisingEvents = true;

            tmrTimeout.Interval = 5000;
            tmrTimeout.Tick+= new EventHandler(tmrTimeout_Tick);
        }

        void fswAuth_Changed(object sender, FileSystemEventArgs e)
        {
            DateTime dtTempReadAt = File.GetLastWriteTime(FILE_PATH);
            if (dtLastResultReadTime < dtTempReadAt)
            {
                dtLastResultReadTime = dtTempReadAt;
                GetAuthorizationStatus();
            }
        }

        private void GetAuthorizationStatus()
        {
            if (InvokeRequired)
            {
                BeginInvoke(new ShowDelegate(GetAuthorizationStatus));
            }
            else
            {
                string strData = ",";
                tmrTimeout.Enabled = false;

                using (FileStream stream = new FileStream(FILE_PATH, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    using (StreamReader streamReader = new StreamReader(stream))
                    {
                        try
                        { strData = streamReader.ReadToEnd(); }
                        catch (Exception)
                        { //log error }
                    }
                }

                string[] arrData = strData.Split(new char[] { ',' });
                if (arrData.Length == 2)
                {

                    if(arrData[0] == this.strID)
                    {
                        if (arrData[1] == "Y")
                        {
                            tmrTimeout.Enabled = false;
                            this.lblWait.Visible = false;
                            this.btnExit.Enabled = true;
                            this.btnLogin.Enabled = false;
                            this.isAuthorized = true;
                            this.strLoginErrorMessage = "";

                            onAuthOccurred(new AuthEventArgs(this.strID, this.dtLoginAt, this.isAuthorized));
                            this.Close();
                        }
                        else
                        {
                            //Authorization failed
                            tmrTimeout.Enabled = false;
                            this.lblWait.Visible = false;
                            this.btnExit.Enabled = true;
                            this.btnLogin.Enabled = true;

                            //also clear local variables
                            this.strID = "";
                            this.dtLoginAt = DateTime.MinValue;
                            this.isAuthorized = false;
                            this.strLoginErrorMessage = "Authorization denied.";
                        }
                    }
                }

                if (!this.isAuthorized && this.strLoginErrorMessage != "")
                {
                    MessageBox.Show(this.strLoginErrorMessage, "Authorization denied", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        void tmrTimeout_Tick(object sender, EventArgs e)
        {
            tmrTimeout.Enabled = false;
            this.lblWait.Visible = false;
            this.btnExit.Enabled = true;
            this.btnLogin.Enabled = true;

            //also clear local variables
            this.strID="";
            this.isAuthorized = false;
            this.dtLoginAt = DateTime.MinValue;

            MessageBox.Show("Timeout occurred while waiting for authorization.", "Authorization timeout", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void onAuthOccurred(AuthEventArgs e)
        {
            EventHandler<AuthEventArgs> handler = AuthOccurred;

            if (handler != null)
            { handler(this, e); }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            this.dtLoginAt = DateTime.Now;
            this.strID = IDGenerator.GetNewID().ToString();

            this.btnLogin.Enabled = false;
            this.btnExit.Enabled = false;
            tmrTimeout.Enabled = true;
            lblWait.Visible = true;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do you wish to cancel login?", "Cancel Login", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                //Clear local variables
                this.strID="";
                this.isAuthorized = false;
                this.dtLoginAt = DateTime.MinValue;
                //raise event and exit
                onAuthOccurred(new AuthEventArgs(this.strID, this.dtLoginAt, this.isAuthorized));
                this.Close();
            }
        }

        void BtnAuthClick(object sender, EventArgs e)
        {
            File.WriteAllText(FILE_PATH,string.Format("{0},Y",this.strID));
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统图;
使用系统文本;
使用System.IO;
使用System.Windows.Forms;
名称空间SampleInvokeTest
{
公共类AuthEventArgs:EventArgs
{
私有字符串strID;
私有日期时间dtLoginAt;
私人布尔授权;
public AuthEventArgs(字符串ID、日期时间登录、bool已授权)
{
this.strID=ID;
this.dtLoginAt=LoginAt;
this.isAuthorized=未授权;
}
公共字符串ID
{get{返回this.strID;}}
公共日期时间登录
{get{返回this.dtLoginAt;}}
公共图书馆被授权了
{get{返回this.isAuthorized;}}
}
公共部分类LoginForm:Form
{
delegate void ShowDelegate();
私有字符串strID=“”;
私有常量字符串文件_PATH=@“E:\test.txt”;
private DateTime DTLastResultTreadTime=DateTime.MinValue;
private DateTime dtLoginAt=DateTime.MinValue;
private bool isAuthorized=假;
私有字符串strLoginErrorMessage=“”;
发生公共事件事件处理程序;
公共登录表单()
{
初始化组件();
FileSystemWatcher fswAuth=新的FileSystemWatcher(Path.GetDirectoryName(FILE_Path));
fswAuth.Filter=Path.GetFileName(文件路径);
fswAuth.NotifyFilter=NotifyFilters.LastWrite;
fswAuth.Changed+=新文件系统验证程序(fswAuth_Changed);
fswAuth.EnableRaisingEvents=true;
tmrTimeout.间隔=5000;
Tick+=新事件处理程序(tmrTimeout_Tick);
}
void fswAuth_已更改(对象发送方、文件系统目标)
{
DateTime dtTempReadAt=File.GetLastWriteTime(文件路径);
if(dtLastResultReadTime     FileSystemWatcher fswAuth = new FileSystemWatcher(...);