Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using C# 5.0 async to read a file

I'm just starting out with C#'s new async features. I've read plenty of how-to's now on parallel downloads etc. but nothing on reading/processing a text file.

I had an old script I use to filter a log file and figured I'd have a go at upgrading it. However I'm unsure if my usage of the new async/await syntax is correct.

In my head I see this reading the file line by line and passing it on for processing in different thread so it can continue without waiting for a result.

Am I thinking about it correctly, or what is the best way to implement this?

static async Task<string[]> FilterLogFile(string fileLocation)
{
    string line;

    List<string> matches = new List<string>();

    using(TextReader file = File.OpenText(fileLocation))
    {        
        while((line = await file.ReadLineAsync()) != null)
        {
            CheckForMatch(line, matches);
        }
    }

    return matches.ToArray();
}

The full script: http://share.linqpad.net/29kgbe.linq

like image 725
Samuel Parkinson Avatar asked Feb 11 '13 12:02

Samuel Parkinson


People also ask

What is C in used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

How do I use C on my computer?

It is a bit more cryptic in its style than some other languages, but you get beyond that fairly quickly. C is what is called a compiled language. This means that once you write your C program, you must run it through a C compiler to turn your program into an executable that the computer can run (execute).

Why do people use C?

The biggest advantage of using C is that it forms the basis for all other programming languages. The mid-level language provides the building blocks of Python, Java, and C++. It's a fundamental programming language that will make it easier for you to learn all other programming languages.

Is C used nowadays?

C exists everywhere in the modern world. A lot of applications, including Microsoft Windows, run on C. Even Python, one of the most popular languages, was built on C. Modern applications add new features implemented using high-level languages, but a lot of their existing functionalities use C.


1 Answers

In my head I see this reading the file line by line and passing it on for processing in different thread so it can continue without waiting for a result.

But that's not what your code does. Instead, you will (asynchronously) return an array when all reading is done. If you actually want to asynchronously return the matches one by one, you would need some sort of asynchronous collection. You could use a block from TPL Dataflow for that. For example:

ISourceBlock<string> FilterLogFile(string fileLocation)
{
    var block = new BufferBlock<string>();

    Task.Run(async () =>
    {
        string line;

        using(TextReader file = File.OpenText(fileLocation))
        {        
            while((line = await file.ReadLineAsync()) != null)
            {
                var match = GetMatch(line);

                if (match != null)
                    block.Post(match);
            }
        }

        block.Complete();
    });

    return block;
}

(You would need to add error handling, probably by faulting the returned block.)

You would then link the returned block to another block that will process the results. Or you could read them directly from the block (by using ReceiveAsync()).


But looking at the full code, I'm not sure this approach would be that useful to you. Because of the way you process the results (grouping and then ordering by count in each group), you can't do much with them until you have all of them.

like image 62
svick Avatar answered Sep 27 '22 20:09

svick