简体   繁体   中英

stopwatch is not working correctly in windows form application

I have a form in which I attached a timer. I want to display a stopwatch on my form and update some values to database every second. I used a windows.form timer to do it. But problem is it makes my application very slow and stopwatch doesn't show time correctly. It takes more than 3-4 seconds to update.

Also when I click another button than timer also interrupted. I need my stopwatch proper working and without delay. what should I do now?

The code is as follows:

public partial class Form2 : Form
{
    Stopwatch sw = new Stopwatch();
    private Timer _timer;
    int count = 0;
    // The last time the timer was started
    private DateTime _startTime = DateTime.MinValue;

    // Time between now and when the timer was started last
    private TimeSpan _currentElapsedTime = TimeSpan.Zero;

    // Time between now and the first time timer was started after a reset
    private TimeSpan _totalElapsedTime = TimeSpan.Zero;

    private TimeSpan timerSinceStartTime = TimeSpan.Zero;

    // Whether or not the timer is currently running
    private bool _timerRunning = false;
    public Form2(String UserName)
    {
        InitializeComponent();
        _timer = new Timer();
        _timer.Interval = 1000;
        _timer.Tick += new EventHandler(timer1_Tick);

        label4.Text = UserName;

    }

    public static class LoginInfo
    {
        public static string UserID;
    }

    private void richTextBox2_TextChanged(object sender, EventArgs e)
    {


    }



    private void button1_Click(object sender, EventArgs e)
    {
        if (!_timerRunning)
        {
            // Set the start time to Now
            _startTime = DateTime.Now;

            // Store the total elapsed time so far
            _totalElapsedTime = _currentElapsedTime;

            _timer.Start();
            _timerRunning = true;
        }
        sw.Start();
        SqlDataReader rd1;
        SqlConnection Con = new SqlConnection("Data Source=69.162.83.242,1232;Initial Catalog=test1;Uid=test;pwd=1234@Test;MultipleActiveResultSets=true;Connect TimeOut=600;");
        //SqlConnection Con = new SqlConnection("Data Source=ADMIN-PC\\YASH;Initial Catalog=test;Integrated Security=True; Connect TimeOut=600");
        Con.Open();
        rd1 = new SqlCommand("Select SName from ExpertChat where email ='" + label4.Text + "'", Con).ExecuteReader();
        rd1.Read();
        string str;
        str = rd1["SName"].ToString();
        rd1.Close();
        string messageMask = "{0} @ {1} : {2}";
        string message = string.Format(messageMask, str, DateTime.Now.ToShortTimeString(), richTextBox2.Text);
        richTextBox1.AppendText(Environment.NewLine + message);
        SqlCommand cmd, cmd1;
        cmd = new SqlCommand("Update Chat set UserInitial=@message where ExpertName ='" + str + "'", Con);
        cmd.Parameters.AddWithValue("@message" ,message); 
        cmd.ExecuteNonQuery();
        cmd1 = new SqlCommand("Update Chat set Updated=1 where ExpertName ='" + str + "'", Con);
        cmd1.ExecuteNonQuery();
        Con.Close();
        richTextBox2.Text = String.Empty;
        richTextBox1.ScrollToCaret();

    }


    private void button3_Click(object sender, EventArgs e)
    {
        sw.Stop();
        _timer.Stop();
        _timerRunning = false;

        // Reset the elapsed time TimeSpan objects
        _totalElapsedTime = TimeSpan.Zero;
        _currentElapsedTime = TimeSpan.Zero;
        label3.Text = _totalElapsedTime.ToString();  
        MessageBox.Show(count.ToString());

        SqlConnection Con = new SqlConnection("Data Source=69.162.83.242,1232;Initial Catalog=test1;Uid=test;pwd=1234@Test;MultipleActiveResultSets=true;Connect TimeOut=600;");
        //SqlConnection Con = new SqlConnection("Data Source=ADMIN-PC\\YASH;Initial Catalog=test;Integrated Security=True; Connect TimeOut=600");
        Con.Open();
        SqlDataReader rd1;
        rd1 = new SqlCommand("Select SName from ExpertChat where email ='" + label4.Text + "'", Con).ExecuteReader();
        rd1.Read();
        string str;
        str = rd1["SName"].ToString();
        rd1.Close();

        SqlDataReader cmd1;
        cmd1 = new SqlCommand("Update Expertchat Set Booked=0 where email='" + label4.Text + "'", Con).ExecuteReader();
        cmd1.Close();
        SqlDataReader cmd2;
        cmd2 = new SqlCommand("Update chat set EndChat=1,ChatTime=" + count + " where ExpertName='" + str + "'", Con).ExecuteReader();
        cmd2.Close();
        count = 0;
        Con.Close();
        this.Close();
    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {

    }

    private void Form2_Load(object sender, EventArgs e)
    {

    }

    private void richTextBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void richTextBox1_TextChanged_1(object sender, EventArgs e)
    {

    }

    private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        SqlConnection Con = new SqlConnection("Data Source=69.162.83.242,1232;Initial Catalog=test1;Uid=test;pwd=1234@Test;MultipleActiveResultSets=true;Connect TimeOut=600;");
        //SqlConnection Con = new SqlConnection("Data Source=ADMIN-PC\\YASH;Initial Catalog=test;Integrated Security=True; Connect TimeOut=600");
        Con.Open();
        SqlDataReader rd1;
        rd1 = new SqlCommand("Select SName from ExpertChat where email ='" + label4.Text + "'", Con).ExecuteReader();
        rd1.Read();
        string str;
        str = rd1["SName"].ToString();
        rd1.Close();
        SqlDataReader rd2 = new SqlCommand("Select top 1 UserName from Chat where ExpertName ='" + str + "'", Con).ExecuteReader();
        rd2.Read();
        string str1;
        str1 = rd2["UserName"].ToString();
        LoginInfo.UserID = str1;
        Con.Close();            
        ClientDetails frm = new ClientDetails(LoginInfo.UserID);
        frm.Show();

    }

    private void label3_Click(object sender, EventArgs e)
    {

    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        count = count + 1;

        timerSinceStartTime = new TimeSpan(timerSinceStartTime.Hours, timerSinceStartTime.Minutes, timerSinceStartTime.Seconds + 1);
        // The current elapsed time is the time since the start button was
        // clicked, plus the total time elapsed since the last reset
        _currentElapsedTime = timerSinceStartTime + _totalElapsedTime;

        // These are just two Label controls which display the current 
        // elapsed time and total elapsed time
        SqlDataReader rd1;
        SqlConnection Con = new SqlConnection("Data Source=69.162.83.242,1232;Initial Catalog=test1;Uid=test;pwd=1234@Test;MultipleActiveResultSets=true;Connect TimeOut=600;");
        //SqlConnection Con = new SqlConnection("Data Source=ADMIN-PC\\YASH;Initial Catalog=test;Integrated Security=True; Connect TimeOut=600");
        Con.Open();

        rd1 = new SqlCommand("Select SName from ExpertChat where email ='" + label4.Text + "'", Con).ExecuteReader();
        rd1.Read();
        string str;
        str = rd1["SName"].ToString();
        rd1.Close();
        SqlDataReader rd;
        rd = new SqlCommand("Select top 1 Data,Updated1 from Chat where ExpertName ='" + str + "' order by id desc", Con).ExecuteReader();
        rd.Read();
        string str5;
        int i;
        str5 = rd["Data"].ToString();
        i = Int32.Parse(rd["Updated1"].ToString());
        rd.Close();
        if (i == 1)
        {
            richTextBox1.AppendText(Environment.NewLine + str5);

            SqlCommand cmd1 = new SqlCommand("Update Chat set Updated1=0 where ExpertName ='" + str + "'", Con);
            cmd1.ExecuteNonQuery();

        }
       label3.Text = timerSinceStartTime.ToString();
        // If we're running on the UI thread, we'll get here, and can safely update 
        // the label's text.

        richTextBox1.ScrollToCaret();




    }
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {


    }

    private void button2_Click(object sender, EventArgs e)
    {

    }
    private void button4_Click(object sender, EventArgs e)
    {

    }


        }

}

The problem is that there is a long running database operation executing in the UI thread. That means that the timer events cannot fire because the UI thread is busy. The solution is to put the long running operation on a different thread, away from the UI.

Your DB code is to slow. Since you're doing this in the UI thread, it's blocking your application. To achieve the desired goal you have to do (at least) two things:

  1. Move the DB code to a background worker , a task or on another thread.

  2. Optimize the code. It's not ok for this query to be so slow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM