【问题标题】:Async file saving is not executing correctly C#异步文件保存未正确执行 C#
【发布时间】:2018-02-14 23:56:23
【问题描述】:

我已经阅读了一些关于 async 和 await 的内容,并认为这将是异步执行文件保存的正确方法,但它似乎仍然没有为我提供我正在寻找的解决方案。

我有一个程序,当出现提示时,它会将照片恢复为其原始、未经编辑的形式。这涉及数据库中的更新,然后从目录中删除文件并将新文件写入目录然后显示。出现的问题(据我所知)是它会在文件完全写入之前更新然后显示。这不会每次都发生,但会在大约 60% 的时间显示旧照片,并且在刷新时会显示正确的照片。这是我在还原操作发生时拥有的当前(相关)代码。

 void RevertPhoto(object sender, EventArgs e)
 {
      //Update Database here

      HandleFileAsync();
 }
 async void HandleFileAsync()
    {
        Task<int> task = ResaveFile();
        int count = await task;
        if(count == 1)
        {
            Thread.Sleep(1000); //I put this in here to see if it'd fix the issue, but I feel like this shouldn't be necessary.
            PicturePlaceholder.Controls.Clear(); //Clear the pictures on the next page
            GetPhotoInfo(Convert.ToInt32(hiddenICTASKID.Value)); //Reload pictures on next page

            showPanels(PanelPhotoReview); //Show the panel while hiding the rest.
            hidePanels(PanelCleanList, PanelSiteList, PanelImageSwap, Panel5, PanelRevertPhoto, PanelImageDelete);
        }
    }

    async Task<int> ResaveFile()
    {
        await Task.Run(() =>
        {
            if (File.Exists(RevertFolderPath.Value)) //These two lines shouldn't be necessary as WriteAllBytes will overwrite.
                File.Delete(RevertFolderPath.Value);

            string filePath = RevertFolderPath.Value;
            File.WriteAllBytes(filePath, Convert.FromBase64String(Base64RevertImage.Value));
        });
        return 1;
    }

这是处理它的正确方法还是我有点不对劲?

【问题讨论】:

  • 不要使用async void
  • 您能否详细说明什么是更好的方法?我使用了这里的示例 2 c-sharpcorner.com/article/async-and-await-in-c-sharp 像我对 int count = await task; 所做的那样返回一个变量是不是很糟糕? ?
  • @Nick async Task HandleFileAsync() 会提高 2000% - 然后调用者可以await 找出发生了什么
  • 将 HandleFileAsync 的内容包装在 try-catch 中,看看是否抛出了一些异常。现在你不知道它是否毫无例外地执行。至于 async void - 你可以让 HandleFileAsync 返回任务,但由于 RevertPhoto 是事件处理程序 - 那么它无论如何都必须是 async void
  • @Marc Gravell / Evk 如果我将方法更改为异步任务 HandleFileAsync() 然后等待 HandleFileAsync();然后它是否不需要使用 Handlefileasync() 方法,我可以在其位置运行 ResaveFile() 方法并将 Clear、Load、Show 内容(Handlefileasync 方法)放在 RevertPhoto 方法的末尾,或者是错误的假设?我还没有完全掌握这个概念。

标签: c# file async-await save


【解决方案1】:

重构你的代码

async void RevertPhoto(object sender, EventArgs e)
 {
      //Update Database here

      await HandleFileAsync();
 }
 async Task HandleFileAsync()
    {
        await ResaveFile();


            PicturePlaceholder.Controls.Clear(); //Clear the pictures on the next page
            GetPhotoInfo(Convert.ToInt32(hiddenICTASKID.Value)); //Reload pictures on next page

            showPanels(PanelPhotoReview); //Show the panel while hiding the rest.
            hidePanels(PanelCleanList, PanelSiteList, PanelImageSwap, Panel5, PanelRevertPhoto, PanelImageDelete);

    }
     async Task ResaveFile()
        {
            await Task.Factory.StartNew(() =>
            {
                if (File.Exists(RevertFolderPath.Value)) //These two lines shouldn't be necessary as WriteAllBytes will overwrite.
                    File.Delete(RevertFolderPath.Value);

                string filePath = RevertFolderPath.Value;
                File.WriteAllBytes(filePath, Convert.FromBase64String(Base64RevertImage.Value));
            });
        }

【讨论】:

  • 不幸的是,这仍然存在保存问题。
猜你喜欢
  • 1970-01-01
  • 2020-07-29
  • 1970-01-01
  • 2021-09-15
  • 2021-11-23
  • 1970-01-01
  • 2014-12-08
  • 2013-10-08
  • 1970-01-01
相关资源
最近更新 更多