2

当我执行按钮单击事件并在打开时写入文本文件时,它仍然挂起 UI 线程的任何原因。在写完之前不能做任何事情。

private async void WriteFileClick(object sender, RoutedEventArgs e)
    {
        string fileName = @"asyn3cfile.txt";
        string text = "Hello async, this was written while you were doing something else in the UI";

        StorageFolder storageFolder = KnownFolders.DocumentsLibrary;
        var writtenFile = await storageFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);

        using (IRandomAccessStream randomAccessStream = await writtenFile.OpenAsync(FileAccessMode.ReadWrite))
        {
            Stream stream = randomAccessStream.AsStreamForWrite();
            stream.Seek(0, SeekOrigin.Begin);
            WriteFileTextAsync(text, stream);
            await stream.FlushAsync();
            await randomAccessStream.FlushAsync();
        }
        Debug.WriteLine("{0} was created", fileName);
    }

    private async static void WriteFileTextAsync(string text, Stream stream)
    {
        for (int i = 0; i < 100; i++)
        {
            var fileText = string.Format("{0} in process {1}\n", text, i);
            byte[] textBytes = Encoding.UTF8.GetBytes(fileText);
            stream.SetLength(stream.Length + textBytes.Length);

            await stream.WriteAsync(textBytes, 0, textBytes.Length);
            new System.Threading.ManualResetEvent(false).WaitOne(100);
        }
    }

XAML

<Button x:Name="WriteFileInDocumentsButton" Content="Write a text file" Click="WriteFileClick"/>

有什么理由吗?

4

2 回答 2

3

你应该改变:

 private async static void WriteFileTextAsync(string text, Stream stream)
{
    for (int i = 0; i < 100; i++)
    {
        var fileText = string.Format("{0} in process {1}\n", text, i);
        byte[] textBytes = Encoding.UTF8.GetBytes(fileText);
        stream.SetLength(stream.Length + textBytes.Length);

        await stream.WriteAsync(textBytes, 0, textBytes.Length);
        new System.Threading.ManualResetEvent(false).WaitOne(100);
    }
}

 private async static Task GetFileTextAsync(string text, Stream stream)
{
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 100; i++)
    {
        sb.AppendLine(string.Format("{0} in process {1}\n", text, i));            
    }
    await stream.WriteAsync(textBytes, 0, textBytes.Length);
}

这将有助于节省一些开销,但更重要的是您必须更改:

 using (IRandomAccessStream randomAccessStream = await writtenFile.OpenAsync(FileAccessMode.ReadWrite))
    {
        Stream stream = randomAccessStream.AsStreamForWrite();
        stream.Seek(0, SeekOrigin.Begin);
        WriteFileTextAsync(text, stream);
        await stream.FlushAsync();
        await randomAccessStream.FlushAsync();
    }
    Debug.WriteLine("{0} was created", fileName);

在 WriteFileAysnc 方法调用中包含 await 关键字。

using (IRandomAccessStream randomAccessStream = await writtenFile.OpenAsync(FileAccessMode.ReadWrite))
    {
        Stream stream = randomAccessStream.AsStreamForWrite();
        stream.Seek(0, SeekOrigin.Begin);
        **await** WriteFileTextAsync(text, stream);
        await stream.FlushAsync();
        await randomAccessStream.FlushAsync();
    }
    Debug.WriteLine("{0} was created", fileName);
于 2012-12-24T14:42:47.367 回答
1

您编写的代码中存在一些错误。您不是在等待 WriteAsync,WriteFileTextAsync 应该返回一个任务,并被等待。

如果您只是编写字符串,您会发现使用FileIO辅助方法更容易。

于 2012-12-23T19:02:50.473 回答