- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用异步调用将文件写入 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/
我尝试编写一个带有超时的简单异步写入代码,如下所示,并期望该函数在给定非常大的缓冲区和小等待时间的情况下抛出 TaskCanceledException。但是,这不会发生。 WriteAsync 将阻
我正在使用异步调用将文件写入 IsolatedStorageFile async void Base64ToImage() { byte[] data; using (IsolatedS
我正在尝试通过TComPort组件异步发送字符串, 但是所有内容都是同步发送的。 问题在于该应用将被阻止,并等待传输结束。 我的代码: procedure TForm1.Button1Click(Se
我有一个 ASP.NET Core API,我为此编写了自定义中间件,以便我可以处理异常并在一个位置写入日志。通过 Kestrel 调试并从浏览器或 postman 提交请求时,中间件按要求工作,但在
我正在尝试调用在模拟HttpResponse 上模拟的WriteAsync,但我不知道要使用的语法。 var responseMock = new Mock(); responseMock.Setup
我正在尝试开发一个基本的 Windows 8 应用商店应用程序,只需创建一个新文件并向其中写入一些示例数据。但是,当我运行我编写的函数时,应用程序挂起并且变得没有响应。我尝试了两种不同的文件创建方法,
您好,如果有人可以向我解释这两种从 Controller 返回数据的方法之间的区别,我正在徘徊。使用 on 方法比其他方法有优势或理由吗? 我猜测返回的函数只是调用 Response.WriteAsy
使用 TcpClient 通过网络推送数据。客户端只发送数据。在某些时候,服务器可能会关闭套接字。服务器关闭套接字后,第一个客户端发送无异常完成。客户端第二次发送抛出 IOException。我本以为
我有一个 asp.net webforms 应用程序,用于从数据库中检索以 varbinary 格式保存的视频并将其显示为 html5 视频标记。 在谷歌搜索后,我找到了一种使用ASP.Net Web
如果我在 Login 调用完成后终止我的服务器,则在调用 stream.WriteAsync(data, 0, data.Count()); 已创建,返回的任务中没有错误指示。 那么,我应该如何检测错
我一直在 Ayende Rahien 的博客上试验一些代码 here它演示了使用异步 HttpHandler 来提高可并发处理的请求数。 不幸的是,我连最基本的例子都无法运行。我收到以下错误: 'Sy
我一直在研究 Asp.Net Security,我发现了一些令人惊讶的代码: 奇怪的代码? context.Response.ContentType = "text/html"; await cont
我必须通过蓝牙与设备进行通信,该设备期望命令由回车+换行分隔。使用 RFCOMMChannel 建立连接。 Atm 似乎我的代码无法正常工作,因为我期待设备的回复,当我使用 simpel 终端程序向它
当有一个 TcpClient 在多个线程中获取其 stream 并在每个线程上对其执行 WriteAsync 时。它是线程安全的吗?数据(传递给 write 方法的字节数组)是否会作为传递给 Writ
我对该代码有疑问, XpsDocument doc = new XpsDocument(_filePath, FileAccess.Read); XpsDocument = doc.GetFixedD
我正在将视频流录制到磁盘。为此,我使用以下代码: private async Task SaveToFile(IntPtr data, Int32 size) { if (_FileStrea
我正在查看 C# 中新的 async 和 await 关键字,只是想感受一下它们。 我正在查看 MSDN FileStream.WriteAsync() example ,而且我不确定我是否理解了什么
我一直在尝试实现一个支持 ReadAsync 和 WriteAsync 的 Stream,并考虑到 documentation 的冗余性,我正在努力了解如何正确执行此操作。具体来说,关于流的光标位置。
在 HttpWebRequest.GetRequestStream() 创建的流上取消 WriteAsync 操作时,我收到一个 WebException 并显示以下消息: Cannot close
异步向客户端写入数据时如何防止这个问题 The BeginWrite method cannot be called when another write operation is pending 我
我是一名优秀的程序员,十分优秀!