gpt4 book ai didi

c# - 异步调用 WriteAsync,我怎么知道它何时完成?

转载 作者:行者123 更新时间:2023-12-05 01:21:13 25 4
gpt4 key购买 nike

我正在使用异步调用将文件写入 IsolatedStorageFile

async void  Base64ToImage()
{
byte[] data;
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isfs = isf.OpenFile("image.p64", FileMode.Open, FileAccess.ReadWrite))
{
data = new byte[isfs.Length];
isfs.Read(data, 0, data.Length);
isfs.Close();
}

using (IsolatedStorageFileStream stream = isf.OpenFile("newReConvertedFile.png", FileMode.Create))
{
await stream.WriteAsync(data, 0, data.Length);

///////////////////The problem is here, how would i know when WriteAsync is complete to then fire the reload method?
//reLoadFile();
}
}
}

我需要知道 WriteAsync 何时完成才能以另一种方法重新加载该文件,我该怎么做?

编辑:

实际上我的第一个想法是将文件保存到 isostorage 然后将图像 uri 发送到该文件,但是你不能将 uri 设置为图像的 isostorage 或者至少我无法显示

我最初的想法是等待方法调用以重新加载文件,但我收到错误消息“???不返回任务且无法等待。考虑将其更改为返回”,但我不确定该怎么做。

这里的想法是将多个图像转换为 Base64(SQLite 数据库),然后仅加载选定的图像以进行显示。

reLoadFile方法如下

async void reLoadFile()
{
byte[] newFile;
using (IsolatedStorageFile newFileLoc = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream newFileStream = newFileLoc.OpenFile("newReConvertedFile.png", FileMode.Open, FileAccess.ReadWrite))
{
newFile = new byte[newFileStream.Length];
newFileStream.Read(newFile, 0, newFile.Length);
newFileStream.Close();

var ms = new MemoryStream(newFile);
var image = new BitmapImage();
image.SetSource(ms);
theImage.Source = image;
}
}
}

我一直在搜索这些帖子,但我找不到让它工作的好方法,因为如果我立即调用它,我会得到一个异常(exception)。我需要做的是打开文件,将其从 base64 转换为 .png,然后在保存后加载 .png 文件。如果有人可以帮助我,我将不胜感激。

谢谢鲍勃。

最佳答案

1) reloadFile 不需要是 async 你只需要在方法中使用 await 关键字时使用它

2) BaseTo64Image 需要声明为 async Task Base64ToImage() 否则 Base64ToImage() 的调用者将无法知道它何时完成。

3) isfs.Read(data, 0, data.Length); 不好,仅仅因为你请求了 data.Length 字节并不意味着你会得到 data.Length读入,需要检查int读取返回并循环或使用CopyTo(Stream)复制到另一个流。

4) (实际回答您的问题)关键字 await 将停止函数处理并等待函数完成。当您点击 await 关键字时,您的 Base64ToImage() 函数将在此时返回一个代表该函数中其余代码的 Task,如何处理函数返回的 Task 取决于您。

这是使用所有建议的代码的简化版本

async Task Base64ToImage()
{
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
using (IsolatedStorageFileStream stream = isf.OpenFile("newReConvertedFile.png", FileMode.Create))
using (IsolatedStorageFileStream isfs = isf.OpenFile("image.p64", FileMode.Open, FileAccess.ReadWrite))
{
await isfs.CopyToAsync(stream);
}

await ReLoadFile();
}

async Task ReLoadFile()
{


using (IsolatedStorageFile newFileLoc = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream newFileStream = newFileLoc.OpenFile("newReConvertedFile.png", FileMode.Open, FileAccess.ReadWrite))
{
var ms = new MemoryStream();
var image = new BitmapImage();
await newFileStream.CopyToAsync(ms);

ms.Position = 0;
image.SetSource(ms);
theImage.Source = image;
}
}
}

但是,如果这就是您所做的全部,并且您只是将 newReConvertedFile.png 用作临时文件,您可以将代码简化为

async Task Base64ToImage()
{
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
using (IsolatedStorageFileStream isfs = isf.OpenFile("image.p64", FileMode.Open, FileAccess.ReadWrite))
{
var ms = new MemoryStream();
var image = new BitmapImage();
await isfs.CopyToAsync(ms);

ms.Position = 0;
image.SetSource(ms);
theImage.Source = image;
}
}

关于c# - 异步调用 WriteAsync,我怎么知道它何时完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21764817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com