Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created July 6, 2015 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fhernd/891de83237e619590fad to your computer and use it in GitHub Desktop.
Save Fhernd/891de83237e619590fad to your computer and use it in GitHub Desktop.
Envío de sañales entre threads.
// OrtizOL - xCSw - http://ortizol.blogspot.com
using System;
using System.Threading;
namespace Receta.Multithreading.R0204
{
public class EnvioNotificacionesThreads
{
private static AutoResetEvent eventoSecundario = new AutoResetEvent(false);
private static AutoResetEvent eventoPrincipal = new AutoResetEvent(false);
public static void Main()
{
Console.WriteLine();
Thread nuevoThread = new Thread( () => Proceso(10));
nuevoThread.Start();
Console.WriteLine ("Thread `Main`: A espera que el nuevo thread finalice su primera tarea.");
// Señal de bloqueo del thread `nuevoThread`:
eventoSecundario.WaitOne();
Console.WriteLine("Thread `Main`: La primera de `Proceso` tarea ha finalizado satisfactoriamente.");
Console.WriteLine("Thread `Main`: Ejecutando una tarea sobre `Main`.");
Thread.Sleep(TimeSpan.FromSeconds(5));
// Señal de desbloqueo del thread `Main`:
eventoPrincipal.Set();
Console.WriteLine("Thread `Main`: En ejecución segunda tarea de `Proceso` sobre el nuevo thread.");
// Señal de bloqueo del thread `nuevoThread`:
eventoSecundario.WaitOne();
Console.WriteLine("Thread `Main`: La segunda operación de `Proceso` ha finalizado.");
Console.WriteLine();
}
public static void Proceso(int segundosEspera)
{
Console.WriteLine("Thread `Proceso`: Inicio de proceso largo...");
Thread.Sleep(TimeSpan.FromSeconds(segundosEspera));
Console.WriteLine("Thread `Proceso`: Primera tarea de `Proceo` finalizada.");
// Señal de desbloqueo del thread `nuevoThread`:
eventoSecundario.Set();
Console.WriteLine("Thread `Proceso`: A espera de que el thread `Main` finalice.");
// Señal de bloqueo del thread `Main`:
eventoPrincipal.WaitOne();
Console.WriteLine("Thread `Proceso`: Inicio segunda operación del proceso...");
Thread.Sleep(TimeSpan.FromSeconds(segundosEspera));
Console.WriteLine("Thread `Proceso`: ¡Proceo finalizado!");
// Señal de desbloqueo thread `nuevoThread`:
eventoSecundario.Set();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment