我开始玩弄任务和异步等待。为了更好地处理如何转换现有代码,我想我会尝试更改同步运行的当前方法:
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;
}
我试图简单地将方法更改为async
, 并涉足,Task<bool>
但我显然在这里遗漏了一些东西,因为它们似乎都不起作用。我所经历Type System.Threading.Task<bool>
的是不可等待的。