Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对WriteAsync的异步调用,我如何知道它何时出现’;完成了吗?_C#_Visual Studio 2012_Asynchronous_Windows Phone 8_Async Await - Fatal编程技术网

C# 对WriteAsync的异步调用,我如何知道它何时出现’;完成了吗?

C# 对WriteAsync的异步调用,我如何知道它何时出现’;完成了吗?,c#,visual-studio-2012,asynchronous,windows-phone-8,async-await,C#,Visual Studio 2012,Asynchronous,Windows Phone 8,Async Await,我正在用异步调用将文件写入IsolatedStorageFile async void Base64ToImage() { byte[] data; using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream isfs = isf.OpenFile("image.p64", FileMode.

我正在用异步调用将文件写入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,或者至少我无法显示

我最初的想法是等待方法调用重新加载文件,但是我得到了一个错误“???不返回一个任务,不能等待。考虑将它更改为返回”,但我不知道该怎么做。< /P> 这里的想法是将多个图像转换为Base64(SQLite db),然后只加载选定的图像以显示

重载文件方法如下所示

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; 
        }
     }
 }
我一直在搜索帖子,但我找不到一个好的方法让它发挥作用,因为如果我马上给它打电话,我会得到一个例外。 我需要做的是打开文件,将其从base64转换为.png,然后在保存后加载.png文件。 如果有人能帮我,我将不胜感激


谢谢你,鲍勃。

下一行就打过来。wait将异步等待,直到
WriteAsync
完成。这就是“async/await”的美妙之处,它允许您编写看起来同步的异步代码

 using (IsolatedStorageFileStream stream = isf.OpenFile("newReConvertedFile.png", FileMode.Create))
 {
     await stream.WriteAsync(data, 0, data.Length);
     reLoadFile();
  }
我错过什么了吗

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

2) BaseTo64Image需要声明为
异步任务Base64ToImage()
,否则
Base64ToImage()
的调用者将无法判断它何时完成

3)
isfs.Read(data,0,data.Length)
不好,只是因为您请求了
数据。长度
字节并不意味着您将获得
数据。长度
读入,您需要检查
int
读取返回并循环,或者使用
复制到(流)
复制到另一个流

4) (实际回答您的问题)关键字
wait
将停止函数的处理,并等待函数完成。当您点击
wait
关键字时,您的
Base64ToImage()
函数将在该点返回一个
Task
表示该函数中其余代码的
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; 
        }
     }
 }
但是,如果这就是您要做的,并且您只是将
newrecovertedfile.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; 
    }      
}

当返回的
任务完成时,
WriteAsync
方法完成

要等待任务完成,只需等待它:

await stream.WriteAsync(data, 0, data.Length);
// WriteAsync is complete.

我不明白为什么这个问题被否决了。它提供了回答此问题所需的所有详细信息。您不应使用async void,请将您的方法更改为asyncTask@vcsjones我也不明白…我试过了,但有个例外,我认为该文件当时可能还没有保存或仍在被访问?@BobMachine只需在using语句之后移动
reLoadFile
调用并检查即可。这应该可以解决您的问题,将其移出使用,这肯定是问题的一部分,thanksim不确定我是否理解您,也许您可以给我看一个例子?在重新加载文件调用中,我在这里遇到一个错误:var ms=new MemoryStream(newFileStream.Length);关于你关于进一步简化的建议,我试图保存它,以便在墓碑上找到它的参考,你认为呢?如果你不告诉我错误是什么,为什么要保存它,我真的帮不了你处理错误,你只是在复制一个文件,然后除了第二次阅读它之外什么也不做。除非您有被排除的代码。错误
参数1:无法从“long”转换为“byte[]”
我仍在处理它,图像将以SQLite db(base64格式)存储,然后根据需要拉出,我正在保存临时文件,在应用程序停用的情况下使用ref。我没有意识到
Length
返回的是
Long
,只需使用无参数构造函数即可。