//Programa 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ThreadsPropiedades
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
Thread t = new Thread(new ThreadStart(p.met1));
t.Start();
//Este método no interrumpe el thread
Console.WriteLine(t.IsAlive);
Console.WriteLine(t.ThreadState);
Thread.Sleep(10000);
Console.WriteLine("-----------------------");
t.Interrupt();
Console.WriteLine(t.ThreadState);
Console.WriteLine("-----------------------");
Thread.Sleep(5000);
Console.WriteLine(t.IsAlive);
Console.WriteLine(t.ThreadState);
Console.ReadKey();
}
public void met1() {
try
{
Thread.Sleep(Timeout.Infinite);
}
catch (ThreadInterruptedException) {
Console.WriteLine("Forzado....");
}
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Despierta........");
}
}
}
//---------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace SeguridadThreadsMutex1
{
class DataBase
{
Mutex mutex = new Mutex();
public void SaveData(string text)
{
mutex.WaitOne();
Console.WriteLine("------------------------------");
Console.WriteLine("DataBase.SaveData: Iniciado.....");
Console.WriteLine("DataBase.SaveData:
Ejecutándose.....");
Console.WriteLine("---------------------------------------");
for (int i = 0; i <= 100; i++)
{
Console.Write(i + text);
}
Console.WriteLine("---------------------------------------");
Console.WriteLine("DataBase.SaveData: Finalizado.....");
mutex.ReleaseMutex();
}
}
class ThreadMutexApp
{
public static DataBase db = new DataBase();
public static void WorkedThreadMethod1()
{
Console.WriteLine("------------------------------------------------------");
Console.WriteLine("Hilo de ejecución secundario #1.-Iniciado..........");
Console.WriteLine("Hilo de ejecución secundario #1.-Invocando DataBase.SaveData");
Console.WriteLine("---------------------------------------------------------------");
db.SaveData("x");
Console.WriteLine("----------------------------------------------------------------");
Console.WriteLine("Hilo de ejecución secundario #1.-Retornando desde Output");
}
public static void WorkedThreadMethod2()
{
Console.WriteLine("-------------------------------------------------------------");
Console.WriteLine("Hilo de ejecución secundario #2.-Iniciado..........");
Console.WriteLine("Hilo de ejecución secundario #2.-Invocando DataBase.SaveData");
Console.WriteLine("--------------------------------------------------------------");
db.SaveData("o");
Console.WriteLine("--------------------------------------------------------------");
Console.WriteLine("Hilo de ejecución secundario #2.-Retornando desde Output");
}
public static void Main()
{
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Green;
Console.Clear();
ThreadStart worker1 = new ThreadStart(WorkedThreadMethod1);
ThreadStart worker2 = new ThreadStart(WorkedThreadMethod2);
Console.WriteLine("Principal.- Creando Hilos de ejecución secundarios......");
Thread t1 = new Thread(worker1);
Thread t2 = new Thread(worker2);
t1.Start();
t2.Start();
Console.ReadKey();
}
}
}
//---------------------En las conclusiones--------------------------------------------------
Nota: Poner qué es lo que hace cada programa. Agregar información teórica a la práctica.
No hay comentarios:
Publicar un comentario