简体   繁体   English

异步文件保存未正确执行 C#

[英]Async file saving is not executing correctly C#

I've read up a bit on async and await and thought this would be the correct way to execute a file save asynchronously, but it still doesn't seem to be giving me the solution i'm looking for.我已经阅读了一些关于 async 和 await 的内容,并认为这将是异步执行文件保存的正确方法,但它似乎仍然没有给我我正在寻找的解决方案。

I have a program that when prompted will revert a photo to its original, unedited form.我有一个程序,当提示时会将照片恢复为原始的、未经编辑的形式。 This involves an update in the database, followed by deleting the file from the directory and writing the new file to the directory to then be displayed.这涉及更新数据库,然后从目录中删除文件并将新文件写入目录以供显示。 The problem that arises (to the best of my knowledge) is that it will update and then display before the file is completely written.出现的问题(据我所知)是它会在文件完全写入之前更新然后显示。 This doesn't happen every time, but will display the old photo about 60% of the time and when refreshed, will show the correct photo.这不会每次都发生,但大约 60% 的时间会显示旧照片,刷新时会显示正确的照片。 Here is the current (relevant) code I have for when the revert action happens.这是我在恢复操作发生时拥有的当前(相关)代码。

 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;
    }

Is this the right way to approach it or am I quite a bit off?这是接近它的正确方法还是我有点偏离?

Refactor your code to将您的代码重构为

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));
            });
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM