消息框弹出不止一次

时间:2014-10-22 17:44:04

标签: c# wpf postgresql timer messagebox

我有以下代码,timerStart方法会每秒调用一些函数,问题是当checkSigning方法中的条件为真时,弹出窗口出现不止一次..我怎么可能解决这个问题?有人可以帮助我:<

  public void timerStart()
    {
        DispatcherTimer updaterTimer = new DispatcherTimer();
        updaterTimer.Tick += new EventHandler(updaterTimer_Tick);
        updaterTimer.Interval = new TimeSpan(0, 0, 1);
        updaterTimer.Start();

    }

    private void updaterTimer_Tick(object sender, EventArgs e)
    {
        Time.Content = "Time : " + DateTime.Now.ToLongTimeString();
        exist = saved_settings();
        if (exist)
        {
            settingForToday();
            checkSigningAvailable();
            setSigning(signingAvailable = getSigning());
        }
        else
        {
            ongoing.Content = "Event : No Event";
            sign_in.Content = "Sign-in Time : ";
            sign_out.Content = "Sign-out Time : ";
        }

    }

    public void checkSigningAvailable()
    {
        if (dt_signing_in.CompareTo(DateTime.Now) < 0)
        {
            if ((!InisOver && signing.Equals("in")) || (!InisOver && signing.Equals("in_out") && !OutisOver))
            {
                disableSigningIn(OutisOver.ToString(),this.event_id);
            }
        }

        if (dt_signing_out.CompareTo(DateTime.Now) < 0)
        {
            if ((!OutisOver && signing.Equals("out")) || (!OutisOver && signing.Equals("in_out") && InisOver))
            {
                disableSigningOut(InisOver.ToString(),this.event_id);

            }
        }
    }


    public void disableSigningOut(string In,string event_id)
    {
            MessageBox.Show("Signing out is over!", "No more signing out!", MessageBoxButton.OK, MessageBoxImage.Information);
            connection.Open();
            string sign = In + ",True";
            string query = "update data_storage set data_details = '" + sign + "' where data_name = 'Signing';";
            NpgsqlCommand command = new NpgsqlCommand(query, connection);
            NpgsqlDataReader dr = command.ExecuteReader();
            dr.Close();
            connection.Close();
            sign_out.Content = "Sign-out Time : Over";
            string query2 = concatQuery(getIDnumberAttendance(event_id));
            updateAbsences(query2);
    }

1 个答案:

答案 0 :(得分:1)

您可以在执行勾选时停止计时器并在结束时再次启动计时器。你唯一的缺点就是从你完成updaterTimer_Tick执行的那一刻开始计算时间 - 但你也可以认为这是一个好处。

private void updaterTimer_Tick(object sender, EventArgs e)
{
    updaterTimer.Stop();
    Time.Content = "Time : " + DateTime.Now.ToLongTimeString();
    exist = saved_settings();
    if (exist)
    {
        settingForToday();
        checkSigningAvailable();
        setSigning(signingAvailable = getSigning());
    }
    else
    {
        ongoing.Content = "Event : No Event";
        sign_in.Content = "Sign-in Time : ";
        sign_out.Content = "Sign-out Time : ";
    }
    updaterTimer.Start();
}