Created
July 19, 2015 22:49
-
-
Save Fhernd/eac569fecdbfafae4bc0 to your computer and use it in GitHub Desktop.
Demostración de lectura asincrónica en C#.
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
// OrtizOL - xCSw - http://ortizol.blogspot.com | |
using System; | |
using System.IO; | |
using System.Threading; | |
namespace Receta.Multithreading.R0301 | |
{ | |
public class DemoLecturaAsincronica | |
{ | |
public static void Main() | |
{ | |
Console.WriteLine(Environment.NewLine); | |
// Creación de archivo de texto para prueba: | |
using (FileStream fs = new FileStream("ArchivoPrueba.txt", FileMode.Create)) | |
{ | |
fs.SetLength(100000); | |
} | |
// Procesamiento asincrónico del archivo de texto en un | |
// thread independiente: | |
ProcesadorLecturaAsincronica plASync = new ProcesadorLecturaAsincronica("ArchivoPrueba.txt"); | |
plASync.IniciarLectura(); | |
// Mientras que el thread de lectura asincrónica se ejecuta, | |
// en `Main` se ejecutan otras tareas: | |
DateTime tiempoInicio = DateTime.Now; | |
while( DateTime.Now.Subtract(tiempoInicio).TotalSeconds < 2) | |
{ | |
Console.WriteLine("[Thread `Main`]: Ejecutando otras operaciones..."); | |
// Pausa para simular una tarea de consumo de | |
// tiempo en `Main`: | |
Thread.Sleep(TimeSpan.FromMilliseconds(100)); | |
} | |
Console.WriteLine("[Thread `Main`]: Finalizado."); | |
Console.ReadLine(); | |
// Eliminación del archivo de prueba: | |
File.Delete("ArchivoPrueba.txt"); | |
Console.WriteLine(Environment.NewLine); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment