简体   繁体   English

将此代码转换为任务以在另一个线程上异步运行

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

I am starting to toy with Tasks and async await. 我开始玩Tasks并等待异步。 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. 我试图将方法简单地更改为async ,并迷惑了Task<bool>但是由于它们似乎都不起作用,我显然在这里丢失了一些东西。 I have experienced Type System.Threading.Task<bool> is not awaitable. 我经历了无法等待的Type System.Threading.Task<bool>

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. 您没有在方法中调用任何异步函数,因此首先将其标记为async是没有任何意义的。 However instead of using the sync CopyTo you can use the async version CopyToAsync . 但是,可以使用异步版本CopyToAsync而不是使用同步CopyTo

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

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

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