Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Task - Asynchronously Reading Files | Asynchronous Programming and Introduction to Web Services
Introduction to .NET with C#

Task - Asynchronously Reading FilesTask - Asynchronously Reading Files

In the previous section, we learned about a file reading method called File.ReadAllText. While it is a synchronous method for reading a file, there is an asynchronous alternative as well - It is called File.ReadAllTextAsync.

There is an attached file in the task description. This is the file we want to read. The base code contains a program that reads the file synchronously.

Task:

Currently, the output of the program is:

Notice how <<ASYNC CHECK>> is only printed after the file is fully read. We want it to print while the file is being read.

Your task is to modify the code and make the program asynchronously read the file.

If the << ASYNC CHECK >> message prints between Started Reading File and Ended Reading File messages, then it means you have successfully implemented an asynchronous solution:

Link to the Task: GitHub
1. Use the async keyword to convert the ReadFile method into an asynchronous method.
2. The return value of the ReadFile method will follow the Task syntax where T will represent the datatype of the return value - In this case the return datatype is string.
3. We need to await the execution of the File.ReadAllTextAsync method inside the ReadFile method.
4. We also need to make the main method asynchronous.
5. Before ending the program, we need to make sure the file reading task is fully performed. For that we can again use the await keyword.

public class AsyncOutput
{
    // Set the file path to the file you downloaded here
    public static string filePath = "your\\file\\path\\here.txt";

    public static async Task Main()
    {
        Console.WriteLine("Started Program");

        Task fileReadingTask = ReadFile(filePath);

        Console.WriteLine("<< ASYNC CHECK >>");

        await fileReadingTask;

        Console.WriteLine("Ended Program");
    }

    public static async Task ReadFile(string fileName)
    {
        Console.WriteLine("Started Reading File");
        string text = await File.ReadAllTextAsync(fileName);
        Console.WriteLine("Ended Reading File");
    }
}

Все було зрозуміло?

Секція 2. Розділ 3
course content

Зміст курсу

Introduction to .NET with C#

Task - Asynchronously Reading FilesTask - Asynchronously Reading Files

In the previous section, we learned about a file reading method called File.ReadAllText. While it is a synchronous method for reading a file, there is an asynchronous alternative as well - It is called File.ReadAllTextAsync.

There is an attached file in the task description. This is the file we want to read. The base code contains a program that reads the file synchronously.

Task:

Currently, the output of the program is:

Notice how <<ASYNC CHECK>> is only printed after the file is fully read. We want it to print while the file is being read.

Your task is to modify the code and make the program asynchronously read the file.

If the << ASYNC CHECK >> message prints between Started Reading File and Ended Reading File messages, then it means you have successfully implemented an asynchronous solution:

Link to the Task: GitHub
1. Use the async keyword to convert the ReadFile method into an asynchronous method.
2. The return value of the ReadFile method will follow the Task syntax where T will represent the datatype of the return value - In this case the return datatype is string.
3. We need to await the execution of the File.ReadAllTextAsync method inside the ReadFile method.
4. We also need to make the main method asynchronous.
5. Before ending the program, we need to make sure the file reading task is fully performed. For that we can again use the await keyword.

public class AsyncOutput
{
    // Set the file path to the file you downloaded here
    public static string filePath = "your\\file\\path\\here.txt";

    public static async Task Main()
    {
        Console.WriteLine("Started Program");

        Task fileReadingTask = ReadFile(filePath);

        Console.WriteLine("<< ASYNC CHECK >>");

        await fileReadingTask;

        Console.WriteLine("Ended Program");
    }

    public static async Task ReadFile(string fileName)
    {
        Console.WriteLine("Started Reading File");
        string text = await File.ReadAllTextAsync(fileName);
        Console.WriteLine("Ended Reading File");
    }
}

Все було зрозуміло?

Секція 2. Розділ 3
some-alt