简体   繁体   中英

C# Timer Tick Event Working Principal

I have ac# form that draws some curves on it. I am trying to draw these curves at a specified interval with random values. I was able to do what I want, but there is a situation that I cannot answer. In the cases below, case 1 has the form repainted very very fast despite the fact that I am using 5 second interval. However, in case 2 if I moved the "Invalidate();" to the other part, then the code works as it is supposed to. What is the reason for that situation?

Thank you...

Case 1:

private void hizlariHesapla()
{
        if (RastgeleDegerCheckBox.Checked == false)
        {
            // Some code blocks

        }
        else
        {
            // Some code blocks
            Invalidate();
        }
        Hesapla(); // Not important for the case
}
private void SurekliCizdir_Tick_1(object sender, EventArgs e)
{
        if (RastgeleDegerCheckBox.Checked == true)
        {

            hizlariHesapla();
        }

}

Case 2:

private void hizlariHesapla()
{
        if (RastgeleDegerCheckBox.Checked == false)
        {
            // Some code blocks

        }
        else
        {
            // Some code blocks

        }
        Hesapla(); // Not important for the case
}
private void SurekliCizdir_Tick_1(object sender, EventArgs e)
{
        if (RastgeleDegerCheckBox.Checked == true)
        {

            hizlariHesapla();
            Invalidate();
        }

}

It sounds as if your Hesapla method can directly or indirectly call back into the hizlariHesapla method. With the Invalidate call outside the loop you will only see it repaint once per timer tick but when it's inside you see the repaint for every time hizlariHesapla is called. Put a breakpoint there and look at the call stack.

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