简体   繁体   中英

Turn this code into a Task to run Asynchronously on another thread

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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