Estoy desarrollando una aplicacion con varias ventanas que ejecutan hilos, por ejemplo, tengo un escaner capa 3 y un TCP Listener escuchando un puerto determinado, resulta que un tiempo despues Windows defender encuentra hilos ejecutandose(en el momento que lo encuentra la aplicacion "no esta usando los hilos por que ya finalizo los procesos que requeria con ese hilo". Pregunto por aqui porque de seguro estoy teniendo una mala practica del uso de hilos.
He desarrollado la aplicacion con Blazor, tambien la he hecho para escritorio con WPF. Y casi siempre Windows defender aparece con ese mensaje.
private void startScannerL3()
{
Data.ScannerL3.IdCounter = 0;
Data.ScannerL3.scannedDevices = new List<ScannerModel>();
Data.ScannerL3.scannerStatus = "Y"; //Iniciado
ShouldRender();
string f = subred;
for (int i = 1; i < 255; i++)
{
//ForceUIToUpdate();
ShouldRender();
Thread x = new Thread(() => Data.ScannerL3.Scan(f, i.ToString()));
x.Start();
}
}
public static void Scan(string subred, string num)
{
Ping _ping;
PingReply _respuesta = null;
IPAddress _ip;
IPHostEntry _host;
string _subred = "." + num;
_ping = new Ping();
_respuesta = _ping.Send(subred + _subred, 100);
if (_respuesta.Status == IPStatus.Success)
{
_ip = IPAddress.Parse(subred + _subred);
ScannerModel data = new ScannerModel { /*hostname = " ",*/ ipAddress = " ", macAddress = " "};
try
{
_host = Dns.GetHostEntry(_ip);
data = new ScannerModel { /*hostname = _host.HostName.ToString(),*/ idDevice = IdCounter.ToString(), ipAddress = _ip.ToString(), macAddress = Methods.GetMacAddress(_ip.ToString()), time = DateTime.Now.ToString("HH:mm:ss"), };
}
catch
{
data = new ScannerModel { /*hostname = "Desconocido",*/ idDevice = IdCounter.ToString(), ipAddress = _ip.ToString(), macAddress = Methods.GetMacAddress(_ip.ToString()), time = DateTime.Now.ToString("HH:mm:ss"), };
}
finally
{
executedThreads++;
if(data != null)
{
scannedDevices.Add(data);
if(executedThreads >= 255)
{
scannerStatus = "F";
}
}
}
}
}
@if (true)
{
Thread x = new Thread(() => LogEvents.Listener(2001, "12345",
_config.GetConnectionString("default")));
x.Start();
}
public static async void Listener(Int32 port, string account, string _config = null)
{
string readdata;
TcpListener server = null;
int IdCounter = 0;
try
{
// Set the TcpListener on port asigned
server = new TcpListener(IPAddress.Any, port);
// Start listening.
server.Start();
// Buffer for reading data
Byte[] bytes = new Byte[256];
bool msgB = false;
// Enter the listening loop.
while (true)
{
TcpClient client = server.AcceptTcpClient();
NetworkStream stream = client.GetStream();
int i;
try
{
// Loop to receive all the data sent by the client.
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate data bytes to a ASCII string.
readdata = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
if (readdata != null)
{
//PROCESOS ASYNC...
}
// Translate data bytes to a ASCII string.
readdata = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
string send = "12345\u0006\r";
if (readdata.IndexOf(accountPanel + " Zs") > -1)
{
send = "\u0006\r";
}
byte[] msge = System.Text.Encoding.ASCII.GetBytes(send);
// Send back a response.
stream.Write(msge, 0, msge.Length);
}
}
// Shutdown and end connection
//client.Close();
}
}
catch (Exception)
{
}
}