Two timers with two different intervals C# Form

2019-04-17 15:44发布

I'm trying to create a chat in c# form and i have created two timers; one of them is checking every 1 second if someone wrote something, the other one is sending every 1 second the message "I'm Online" or "I'm offline" depends on what i have set; the problem is that i'm spamming too much even if i did an if which doesn't show anymore the spam messages; but the problem is even if i don't see the messages, the chat is lagging, i mean more time i let it open more lag it does.. for example if i write a message from another console, i see it after 30 seconds, and the time increases if let it open. So i tried to change the interval of the timer which sends "ON" or "OFF" to 1 minute, but when i try to start the program it crashes, it works only if the timers have the same intervals and i don't understand why, i'm looking for a solution...

MY PROBLEM IS THAT I WANT TO PUT DIFFERENT TIME INTERVALS, AND WHEN I DO THAT THE PROGRAM CRASHES, IT'S FREEZING.. IT DOESN'T WORK ANYORE.

-> i want to let the check timer as default to 100 ms, and to set the status sender timer to 60000(1 minute); but if i do that the program freezes; it doesn't give me an error, but i can't do anything anymore.

Timers initializing:

private void Form1_Load(object sender, EventArgs e)
        {
            ControlTimerInbox.Enabled = true;
            ControlTimerInbox.Start();
            StatusTimer.Enabled = true;
            StatusTimer.Start();


        }

This one is for the CONTROL TIMER:

private void ControlTimerInbox_Tick(object sender, EventArgs e)
        {
            mesric = Encoding.ASCII.GetString(receive.Receive(ref ipremoto));
            if (mesric.Contains("OFF!")==true)
            {
                if (mesric.Contains("GruppoUno") == true)
                {

                    GruppoUnoStatusPicture.BackgroundImage = Image.FromFile(@".\tvchat\statusoff.jpg");
                    GruppoUnoLblStatus.ForeColor = Color.Red;

                }
                else
                if (mesric.Contains("GruppoDue") == true)
                {
                    GruppoDueStatusPicture.BackgroundImage = Image.FromFile(@".\tvchat\statusoff.jpg");
                    GruppoDueLblStatus.ForeColor = Color.Red;
                }
                else
                if (mesric.Contains("GruppoTre") == true)
                {
                    GruppoTreStatusPicture.BackgroundImage = Image.FromFile(@".\tvchat\statusoff.jpg");
                    GruppoTreLblStatus.ForeColor = Color.Red;
                }
                else
                if (mesric.Contains("GruppoQuattro") == true)
                {
                    GruppoQuattroStatusPicture.BackgroundImage = Image.FromFile(@".\tvchat\statusoff.jpg");
                    GruppoQuattroLblStatus.ForeColor = Color.Red;

                }
                else
                if (mesric.Contains("GruppoCinque") == true)
                {
                    GruppoCinqueStatusPicture.BackgroundImage = Image.FromFile(@".\tvchat\statusoff.jpg");
                    GruppoCinqueLblStatus.ForeColor = Color.Red;

                }

            }
            else
                 if (mesric.Contains("ON!")==true)
                 {
                     if (mesric.Contains("GruppoUno") == true)
                     {
                         GruppoUnoStatusPicture.BackgroundImage = Image.FromFile(@".\tvchat\statuson.jpg");
                         GruppoUnoLblStatus.ForeColor = Color.Green;
                     }
                     else
                         if (mesric.Contains("GruppoDue") == true)
                         {
                             GruppoDueStatusPicture.BackgroundImage = Image.FromFile(@".\tvchat\statuson.jpg");
                             GruppoDueLblStatus.ForeColor = Color.Green;
                         }
                         else
                             if (mesric.Contains("GruppoTre") == true)
                             {
                                 GruppoTreStatusPicture.BackgroundImage = Image.FromFile(@".\tvchat\statuson.jpg");
                                 GruppoTreLblStatus.ForeColor = Color.Green;
                             }
                             else
                                 if (mesric.Contains("GruppoQuattro") == true)
                                 {
                                     GruppoQuattroStatusPicture.BackgroundImage = Image.FromFile(@".\tvchat\statuson.jpg");
                                     GruppoQuattroLblStatus.ForeColor = Color.Green;
                                 }
                                 else
                                     if (mesric.Contains("GruppoCinque") == true)
                                     {
                                         GruppoCinqueStatusPicture.BackgroundImage = Image.FromFile(@".\tvchat\statuson.jpg");
                                         GruppoCinqueLblStatus.ForeColor = Color.Green;

                                     }
                 }
            else
            {
            nome = Dns.GetHostByAddress(ipremoto.Address);
            CampoChat.Text += "(" + DateTime.Now + ")" + mesric + "\n";
            ControlTimerInbox.Enabled = true;
            ControlTimerInbox.Start();  
            }

THIS ONE IS FOR SENDING THE STATUS:

private void StatusTimer_Tick(object sender, EventArgs e)
        {
            if (status == true)
            {                
                    mes = "GruppoDue:ON!";
                    send.Send(Encoding.ASCII.GetBytes(mes), mes.Length, "192.168.7.255", 11000);                 
            }
            else
            {              
                    mes = "GruppoDue:OFF!";
                    send.Send(Encoding.ASCII.GetBytes(mes), mes.Length, "192.168.7.255", 11000);
            }
            StatusTimer.Start();

1条回答
冷血范
2楼-- · 2019-04-17 16:08

from your comments:

you need to set 1000 for 1 sec as 1 sec = 1000 milliseconds. because by default Interval is 100

private void Form1_Load(object sender, EventArgs e)
        {
            ControlTimerInbox.Interval=1000;//for 1 second
            ControlTimerInbox.Enabled = true;
            ControlTimerInbox.Start();

            StatusTimer.Interval=1000;//for 1 second
            StatusTimer.Enabled = true;
            StatusTimer.Start();


        }

Solution 2: You don't need to call Start() until unless You call Stop().

So remove calling Start()

Controller Timer

 else
            {
            nome = Dns.GetHostByAddress(ipremoto.Address);
            CampoChat.Text += "(" + DateTime.Now + ")" + mesric + "\n";
            ControlTimerInbox.Enabled = true;
            //ControlTimerInbox.Start();  //remove or comment
            }

Sending Status:

private void StatusTimer_Tick(object sender, EventArgs e)
        {
            if (status == true)
            {                
                    mes = "GruppoDue:ON!";
                    send.Send(Encoding.ASCII.GetBytes(mes), mes.Length, "192.168.7.255", 11000);                 
            }
            else
            {              
                    mes = "GruppoDue:OFF!";
                    send.Send(Encoding.ASCII.GetBytes(mes), mes.Length, "192.168.7.255", 11000);
            }
              //StatusTimer.Start();//remove or comment
查看更多
登录 后发表回答