30 Ağustos 2018 Perşembe

c# ReadTextFileAsync. txt okuma

ReadTextFileAsync.cs
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;

public class ReadTextFileAsync
{
    static void Main()
    {    
        Task task = new Task(ReadMyFile);
        task.Start();
        
        Console.ReadLine();
    }
    
    static async void ReadMyFile() {
    
        FileStream fs = new FileStream("thermopylae.txt", 
                FileMode.Open, FileAccess.Read);
        
        using (StreamReader reader = new StreamReader(fs, Encoding.UTF8))
        {
            string text = await reader.ReadToEndAsync();
            Console.WriteLine(text);
        }    
    }
}
In the next example, we read a text file asynchronously.
Task task = new Task(ReadMyFile);
task.Start();
Task class represents an asynchronous operation.
string text = await reader.ReadToEndAsync();

Hiç yorum yok:

Yorum Gönder