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 syncCopyTo
you can use the async versionCopyToAsync
... And without async/await: