• 转 Using Async for File Access


    原文:https://msdn.microsoft.com/en-us/library/jj155757.aspx

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Text;
    using System.Threading.Tasks;
    Writing Text

    The following example writes text to a file. At each await statement, the method immediately exits. When the file I/O is complete, the method resumes at the statement that follows the await statement. Note that the async modifier is in the definition of methods that use the await statement.

    public async void ProcessWrite()
    {
        string filePath = @"temp2.txt";
        string text = "Hello World
    ";
    
        await WriteTextAsync(filePath, text);
    }
    
    private async Task WriteTextAsync(string filePath, string text)
    {
        byte[] encodedText = Encoding.Unicode.GetBytes(text);
    
        using (FileStream sourceStream = new FileStream(filePath,
            FileMode.Append, FileAccess.Write, FileShare.None,
            bufferSize: 4096, useAsync: true))
        {
            await sourceStream.WriteAsync(encodedText, 0, encodedText.Length);
        };
    }

    The original example has the statement await sourceStream.WriteAsync(encodedText, 0, encodedText.Length);, which is a contraction of the following two statements:

    Task theTask = sourceStream.WriteAsync(encodedText, 0, encodedText.Length);
    await theTask;

    The first statement returns a task and causes file processing to start. The second statement with the await causes the method to immediately exit and return a different task. When the file processing later completes, execution returns to the statement that follows the await. For more information, see Control Flow in Async Programs (C# and Visual Basic) and Walkthrough: Using the Debugger with Async Methods.

    Reading Text

    The following example reads text from a file. The text is buffered and, in this case, placed into a StringBuilder. Unlike in the previous example, the evaluation of the await produces a value. The ReadAsync method returns a Task<Int32>, so the evaluation of the await produces an Int32 value (numRead) after the operation completes. For more information, see Async Return Types (C# and Visual Basic).

    public async void ProcessRead()
    {
        string filePath = @"temp2.txt";
    
        if (File.Exists(filePath) == false)
        {
            Debug.WriteLine("file not found: " + filePath);
        }
        else
        {
            try
            {
                string text = await ReadTextAsync(filePath);
                Debug.WriteLine(text);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
    }
    
    private async Task<string> ReadTextAsync(string filePath)
    {
        using (FileStream sourceStream = new FileStream(filePath,
            FileMode.Open, FileAccess.Read, FileShare.Read,
            bufferSize: 4096, useAsync: true))
        {
            StringBuilder sb = new StringBuilder();
    
            byte[] buffer = new byte[0x1000];
            int numRead;
            while ((numRead = await sourceStream.ReadAsync(buffer, 0, buffer.Length)) != 0)
            {
                string text = Encoding.Unicode.GetString(buffer, 0, numRead);
                sb.Append(text);
            }
    
            return sb.ToString();
        }
    }
     
    Parallel Asynchronous I/O

    The following example demonstrates parallel processing by writing 10 text files. For each file, the WriteAsync method returns a task that is then added to a list of tasks. The await Task.WhenAll(tasks); statement exits the method and resumes within the method when file processing is complete for all of the tasks.

    The example closes all FileStream instances in a finally block after the tasks are complete. If each FileStream was instead created in a using statement, the FileStream might be disposed of before the task was complete.

    Note that any performance boost is almost entirely from the parallel processing and not the asynchronous processing. The advantages of asynchrony are that it doesn’t tie up multiple threads, and that it doesn’t tie up the user interface thread.

    public async void ProcessWriteMult()
    {
        string folder = @"tempfolder";
        List<Task> tasks = new List<Task>();
        List<FileStream> sourceStreams = new List<FileStream>();
    
        try
        {
            for (int index = 1; index <= 10; index++)
            {
                string text = "In file " + index.ToString() + "
    ";
    
                string fileName = "thefile" + index.ToString("00") + ".txt";
                string filePath = folder + fileName;
    
                byte[] encodedText = Encoding.Unicode.GetBytes(text);
    
                FileStream sourceStream = new FileStream(filePath,
                    FileMode.Append, FileAccess.Write, FileShare.None,
                    bufferSize: 4096, useAsync: true);
    
                Task theTask = sourceStream.WriteAsync(encodedText, 0, encodedText.Length);
                sourceStreams.Add(sourceStream);
    
                tasks.Add(theTask);
            }
    
            await Task.WhenAll(tasks);
        }
    
        finally
        {
            foreach (FileStream sourceStream in sourceStreams)
            {
                sourceStream.Close();
            }
        }
    }

    When using the WriteAsync and ReadAsync methods, you can specify a CancellationToken, which you can use to cancel the operation mid-stream. For more information, see Fine-Tuning Your Async Application (C# and Visual Basic) and Cancellation in Managed Threads.

  • 相关阅读:
    deepinmind(转)
    不知道数据库中表的列类型的前提下,使用JDBC正确的取出数据(转)
    shell 监控局域网的主机是否up(转)
    IntelliJ Idea中一个编译报错引发的
    Unity插件之NGUI学习(8)—— Table和NGUI尺寸转换为世界坐标系尺寸
    使用php-fpm状态页观察当前的php-fpm状态
    PHP连接Access数据库代码
    HDU 4107 线段树
    Effective C++之‘宁以pass-by-reference-to-const替换pass-by-value’
    xdebug的安装和配置方法
  • 原文地址:https://www.cnblogs.com/ly7454/p/4781050.html
Copyright © 2020-2023  润新知