函数不应该写入DB

时间:2016-07-15 13:45:36

标签: c# sql

我有一个间隔为1秒的Timer。每秒我都会调用此函数来检查操作系统的最后输入时间。如果函数返回的时间超过600(10分钟),我会调用另一个函数将内容写入数据库。

问题出在这里,而不是在DB中插入一行,该函数将在同一秒内创建许多记录。我不确定为什么会这样。调试时不会发生这种情况。

public void elapsedGetIdleCount(object source, ElapsedEventArgs e)
    {
        uint result = GetLastInputTime();

        if (result >= 600)
        {
            result = 0;
            tmrIdle.Stop();

            try
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(@"C:/Users/" + Environment.UserName + "/AppData/Roaming/3CXPhone for Windows/3CXPhone.xml");
                var element = xDoc.SelectSingleNode("Accounts/Profiles/Profile/AuthUser");
                var extension = element.InnerText.ToString();

                SetQueueStatus("LoggedOUT", "Inactivity");

                DialogResult mb = MessageBox.Show("You have been Logged Out of the Queues due to inactivity!", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                if (mb == DialogResult.OK)
                {
                    ChangeQueueStatusColor();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

public void SetQueueStatus(string status, string eventDesc)
    {
        try
        { 
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(@"C:/Users/" + Environment.UserName + "/AppData/Roaming/3CXPhone for Windows/3CXPhone.xml");
            var element = xDoc.SelectSingleNode("Accounts/Profiles/Profile/AuthUser");
            var extension = element.InnerText.ToString();

            string conString = "Data Source = lewcomp1\\COMPLIANCE; Initial Catalog = ComplianceData; Integrated Security = True";

            using (SqlConnection myCon = new SqlConnection(conString))
            {
                using (SqlCommand myCMD = new SqlCommand())
                {
                    myCMD.Connection = myCon;
                    myCMD.CommandText = "UPDATE eData SET QueueStatus = '" + status + "', Extension = '" + extension + "' WHERE UserName LIKE '" + lblUserName.Text + "';";
                    SqlDataReader myReader;

                    myCon.Open();
                    myReader = myCMD.ExecuteReader();
                    myReader.Read();
                    myCon.Close();

                    myCMD.CommandText = "INSERT INTO eQueueData (Date_Time, UserName, Extension, EventID, EventDesc) VALUES ('" + DateTime.Now + "','" + lblUserName.Text + "','" + extension + "','" + status + "','" + eventDesc + "');";
                    myCon.Open();
                    myReader = myCMD.ExecuteReader();
                    myReader.Read();
                    myCon.Close();
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

static uint GetLastInputTime()
    {
        uint idleTime = 0;
        LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
        lastInputInfo.cbSize = (uint)Marshal.SizeOf(lastInputInfo);
        lastInputInfo.dwTime = 0;

        uint envTicks = (uint)Environment.TickCount;

        if (GetLastInputInfo(ref lastInputInfo))
        {
            uint lastInputTick = lastInputInfo.dwTime;

            idleTime = envTicks - lastInputTick;
        }
        return ((idleTime > 0) ? (idleTime / 1000) : 0);
    }

Screen Shot of DB Entries

1 个答案:

答案 0 :(得分:0)

从你的代码中我很确定,这个例程被多次调用。可能的原因:

  • 多次取消活动
  • 有几个地方独立调用

放置一个断点并观察调用堆栈......

一个想法可能是使用静态标记来标记,由于不活动而退出的过程正在运行,并拒绝再次进入该过程。

另一个想法是为lastInputTick设置一个值,以确保下一个电话的回答低于600 ......