此性能注意事项非常重要。 windows Metro style app 或 desktop app 耗时的流操作会阻止 UI 线程并将您的应用程序显示的位置,就象它不起作用。

TextWriter的类。

这些方法可在 .NET Framework 4.5 RC 支持旧版代码;但是,异步方法帮助您更轻松地实现异步 I/O 操作。

从开始 Visual Studio 2012 RC, Visual Studio 为异步编程提供两个关键字:

  • async 修饰符,用于指示方案包含一个异步操作。

  • await 运算符,会应用于异步方法的结果。

异步编程与异步和等待 (C# 和 Visual Basic)

async 修饰符,因为它调用异步方法。

 

using System;
using System.Threading.Tasks;
using System.Windows;
using System.IO;

namespace WpfApplication
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            string StartDirectory = @"c:\Users\exampleuser\start";
            string EndDirectory = @"c:\Users\exampleuser\end";

            foreach (string filename in Directory.EnumerateFiles(StartDirectory))
            {
                using (FileStream SourceStream = File.Open(filename, FileMode.Open))
                {
                    using (FileStream DestinationStream = File.Create(EndDirectory + filename.Substring(filename.LastIndexOf('\\'))))
                    {
                        await SourceStream.CopyToAsync(DestinationStream);
                    }
                }
            }
        }
    }
}

 

StreamWriter 对象读取和写入文本文件的内容异步。

 

private async void Button_Click(object sender, RoutedEventArgs e)
{
    string UserDirectory = @"c:\Users\exampleuser\";

    using (StreamReader SourceReader = File.OpenText(UserDirectory + "BigFile.txt"))
    {
        using (StreamWriter DestinationWriter = File.CreateText(UserDirectory + "CopiedFile.txt"))
        {
            await CopyFilesAsync(SourceReader, DestinationWriter);
        }
    }
}

public async Task CopyFilesAsync(StreamReader Source, StreamWriter Destination) 
{ 
    char[] buffer = new char[0x1000]; 
    int numRead; 
    while ((numRead = await Source.ReadAsync(buffer, 0, buffer.Length)) != 0) 
    {
        await Destination.WriteAsync(buffer, 0, numRead);
    } 
} 

 

它使用异步方法打开文件以流和读取其内容。

 

                     using System;
using System.IO;
using System.Text;
using Windows.Storage.Pickers;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace ExampleApplication
{
    publicsealedpartialclass BlankPage : Page
    {
        public BlankPage()
        {
            this.InitializeComponent();
        }

        privateasyncvoid Button_Click_1(object sender, RoutedEventArgs e)
        {
            StringBuilder contents = new StringBuilder();
            string nextLine;
            int lineCounter = 1;

            var openPicker = new FileOpenPicker();
            openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
            openPicker.FileTypeFilter.Add(".txt");
            StorageFile selectedFile = await openPicker.PickSingleFileAsync();

            using (StreamReader reader = new StreamReader(await selectedFile.OpenStreamForReadAsync()))
            {
                while ((nextLine = await reader.ReadLineAsync()) != null)
                {
                    contents.AppendFormat("{0}. ", lineCounter);
                    contents.Append(nextLine);
                    contents.AppendLine();
                    lineCounter++;
                    if (lineCounter > 3)
                    {
                        contents.AppendLine("Only first 3 lines shown.");
                        break;
                    }
                }
            }
            DisplayContentsBlock.Text = contents.ToString();
        }
    }
}

 

 

 

 

 

相关文章:

  • 2021-07-15
  • 2021-12-23
  • 2021-06-24
  • 2021-08-31
  • 2022-01-19
  • 2021-09-01
  • 2021-09-11
  • 2021-12-04
猜你喜欢
  • 2021-07-12
  • 2021-04-26
  • 2021-05-26
  • 2022-12-23
  • 2021-07-27
  • 2021-11-13
相关资源
相似解决方案