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);
}
}
}