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();
A
Task class represents an asynchronous operation.string text = await reader.ReadToEndAsync();
Hiç yorum yok:
Yorum Gönder