El código que he puesto es un bucle que recorre una matriz cargada con hilos que están funcionando y que quiere saber cuántos hilos siguen vivos [IsAlive] La comprobación de [Nothing] es para evitar errores por llamar a una referencia nula (cuando el hilo termina su ejecución me lo cargo haciéndolo [Nothing]
Ya sé que he descubierto la pólvora, pero me gusta, y estoy mu contento :-)
Dim resultado As Integer = 0 If Not (_matrizHilos Is Nothing) Then '---------------------------------- ' Usando código normal 'For indice As Integer = _matrizHilos.GetLowerBound(0) To _matrizHilos.GetUpperBound(0) ' cuerpo del bucle ' If Not (_matrizHilos(indice) Is Nothing) Then ' If _matrizHilos(indice).IsAlive = True Then ' resultado += 1 ' End If ' End If 'Next '---------------------------------- ' Usando [Paralelismo de datos (Task Parallel Library)] ' http://msdn.microsoft.com/es-es/library/dd537608.aspx> Threading.Tasks.Parallel.For(_matrizHilos.GetLowerBound(0), _ _matrizHilos.GetUpperBound(0), _ Sub(indice) 'cuerpo del bucle If Not (_matrizHilos(indice) Is Nothing) Then If _matrizHilos(indice).IsAlive = True Then resultado += 1 End If End If End Sub) End If ' Return resultado
Ta otra amig@
No hay comentarios:
Publicar un comentario