Turn this code into a Task to run Asynchronously o

2019-08-28 02:25发布

问题:

I am starting to toy with Tasks and async await. So that I get a better handle on how to convert my existing code, I thought I would try to try changing a current method which is run synchronously:

private bool PutFile(FileStream source, string destRemoteFilename, bool overwrite)
{
    if (string.IsNullOrEmpty(destRemoteFilename)) return false;
    string path = Path.GetDirectoryName(destRemoteFilename);
    if (path == null) return false;
    if (!Directory.Exists(path)) Directory.CreateDirectory(path);
    if (overwrite)
    {
        if (File.Exists(destRemoteFilename)) //delete file if it exists, because we are going to write a new one                 File.Delete(destRemoteFilename);
    }
    else if (File.Exists(destRemoteFilename)) return false;
    using (FileStream dest = File.OpenWrite(destRemoteFilename))
    {
        source.Seek(0, SeekOrigin.Begin);
        source.CopyTo(dest);
    }
    return true;
}

I have tried to simply change the method to async, and dabbled with Task<bool> but I'm obviously missing something here as neither of them seem to work. I have experienced Type System.Threading.Task<bool> is not awaitable.

回答1:

You're not calling any async functions in your method, so it would not make any sense to mark it async in the first place. However instead of using the sync CopyTo you can use the async version CopyToAsync.

private static async Task<bool> PutFile(FileStream source, string destRemoteFilename, bool overwrite)
{
    if(string.IsNullOrWhiteSpace(destRemoteFilename))
        return false;

    var path = Path.GetDirectoryName(destRemoteFilename);

    if(path == null) 
        return false;

    if(!Directory.Exists(path))
        Directory.CreateDirectory(path);

    if (overwrite)
        if (!File.Exists(destRemoteFilename))
            return false;
        else
            File.Delete(destRemoteFilename);

    using (var dest = File.OpenWrite(destRemoteFilename))
    {
        source.Seek(0, SeekOrigin.Begin);
        await source.CopyToAsync(dest);

        return true;
    }
}

.. And without async/await:

private static Task<bool> PutFile(FileStream source, string destRemoteFilename, bool overwrite)
{
    if (string.IsNullOrWhiteSpace(destRemoteFilename))
        return Task.Factory.StartNew(() => false);

    var path = Path.GetDirectoryName(destRemoteFilename);

    if(path == null)
        return Task.Factory.StartNew(() => false);

    if(!Directory.Exists(path))
        Directory.CreateDirectory(path);

    if (overwrite)
        if (!File.Exists(destRemoteFilename))
            return Task.Factory.StartNew(() => false);
        else
            File.Delete(destRemoteFilename);

    using (var dest = File.OpenWrite(destRemoteFilename))
    {
        source.Seek(0, SeekOrigin.Begin);
        return source.CopyToAsync(dest).ContinueWith(x => true);
    }
}