Memory problem with a Threading.Timer pinging

1

Good morning, I just started in the world of C # and I'm having some problem.

The application works fine, but the memory consumption grows too much and reaches a point that ends up overflowing.

It is a form, which every 10 seconds consults 3 IP addresses by pinging and returns if it is good or not. All this is done with a Timer. The check can also be done with 3 buttons.

The problem is that I comment, that the use of memory does not stop growing, and I can not find a way to stop it.

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Net.NetworkInformation;

namespace EjemploPing
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            System.Threading.Timer tmr = new System.Threading.Timer(Tick, "tick...", 10000, 1000);


        }
        public void Tick(object data)
        {
            // Esto se ejecuta en un hilo combinado
            //ComprobarPing("192.168.0.173", label1);
            ejecutar();


        }


        private void ejecutar()
        {
            ComprobarPing("192.168.0.110", label1);
            ComprobarPing("192.168.0.249", label2);
            ComprobarPing("192.168.0.248", label3);
        }

        public Boolean ComprobarPing(String ip,Label lb)
        {
            if ((new Ping().Send(ip, 2).Status.ToString()).Equals("Success"))
            {

               lb.BackColor = Color.Green;
               return true;
            }
            else
            {
                lb.BackColor = Color.Red;
                return false;
            }
        }
        private void Comprobar1_Click(object sender, EventArgs e)
        {
            ComprobarPing("192.168.0.110",label1);
        }

        private void Comprobar2_Click(object sender, EventArgs e)
        {
            ComprobarPing("192.168.0.249", label2);
        }

        private void Comprobar3_Click(object sender, EventArgs e)
        {
            ComprobarPing("192.168.0.248", label3);
        }
    }
}
    
asked by Sergio Cv 17.03.2017 в 10:30
source

1 answer

3

In principle, your code is very simple and the only problem I can appreciate is that every time you are creating a Ping object that may not be releasing resources correctly. In all objects of type IDisposable it is always good to use a structure using in such a way that when finishing with the instance, all resources are released.

In your case I would try to modify the ComprobarPing method in the following way:

public Boolean ComprobarPing(String ip, Label lb)
{
    using (var ping = new Ping())
    { 
        if (ping.Send(ip, 2).Status.ToString().Equals("Success"))
        {

            lb.BackColor = Color.Green;
            return true;
        }
        else
        {
            lb.BackColor = Color.Red;
            return false;
        }
    }
}

Try this to see if this solves your memory problem.

    
answered by 17.03.2017 в 10:48