Created
June 27, 2014 04:11
-
-
Save Fhernd/8764d9217dfbb0f28b54 to your computer and use it in GitHub Desktop.
Demostración 'inmortalidad' de un thread ante múltiples intentos de abortar su ejecución.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
namespace Recetas.Multithreading.Cap01 | |
{ | |
internal class InmortalidadThread | |
{ | |
public static void Main() | |
{ | |
Thread nuevoThread = new Thread(EjecutarTarea); | |
nuevoThread.Start(); | |
// Intento no. 1 de abortar el thread nuevoThread: | |
Thread.Sleep (1000); | |
nuevoThread.Abort(); | |
// Intento no. 2 de abortar el thread nuevoThread: | |
Thread.Sleep (1000); | |
nuevoThread.Abort(); | |
// Intento no. 3 de abortar el thread nuevoThread: | |
Thread.Sleep (1000); | |
nuevoThread.Abort(); | |
} | |
private static void EjecutarTarea() | |
{ | |
while (true) | |
{ | |
try | |
{ | |
while (true); | |
} | |
catch (ThreadAbortException) | |
{ | |
// Recuperación de la excepción | |
// ThreadAbortException: | |
Thread.ResetAbort(); | |
} | |
Console.WriteLine ("El thread permanece activo..."); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment